在 test.hs 中,我有:
doubleMe x = x + x
在 ghci 中,我输入:
Prelude> :l test
[1 of 1] Compiling Main ( test.hs, interpreted )
Ok, modules loaded: Main.
*Main> doubleMe 9
<interactive>:1:0: Not in scope: `doubleMe'
*Main>
为什么?怎么修?
我的猜测是您已经在源文件中定义了一个 main 函数。
如果您定义了一个main
函数,加载模块:l test
不会导入任何函数,但是main
。在这种情况下,您可以通过在模块名称前添加星号来加载它::l *test
。
原因是编译后的二进制文件会隐藏未导出的顶级函数。前面加上星号会强制 GHCi 忽略预编译模块 (test) 并解释源文件 (test.hs)。
[jkramer/sgi5k:.../haskell]# cat test.hs
main = do
print $ doubleMe 2
doubleMe x = x + x
[jkramer/sgi5k:.../haskell]# ghc --make test
[jkramer/sgi5k:.../haskell]# ghci
[...some messages...]
>> :l test
Ok, modules loaded: Main.
>> :t doubleMe
<interactive>:1:0: Not in scope: `doubleMe'
>> :l *test
[1 of 1] Compiling Main ( test.hs, interpreted )
Ok, modules loaded: Main.
>> :t doubleMe
doubleMe :: (Num a) => a -> a
检查这些链接以获取更多信息:
http://www.haskell.org/ghc/docs/6.12.2/html/users_guide/ghci-compiled.html http://www.haskell.org/ghc/docs/6.12.2/html/users_guide/ghci-compiled.html
http://www.haskell.org/ghc/docs/6.12.2/html/users_guide/interactive-evaluation.html#ghci-scope http://www.haskell.org/ghc/docs/6.12.2/html/users_guide/interactive-evaluation.html#ghci-scope
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)