在 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
函数,加载模块: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/interactive -evaluation.html#ghci-scope
从目录中删除 test.hi 和 test.o,然后尝试ghci test
. [有时当我运行ghc file.hs
(而不是)时,它会给出未定义的引用错误,但会创建稍后ghc --make file.hs
读取的此类文件。ghci
也许这是一个错误。]
尝试
:cd "<path to your file>"
:l test
:browse
在 ghci 中。结果是什么?
你确定你正在加载正确的 test.hs 吗?也许你在错误的目录中。或者你可能在添加doubleMe的定义后没有保存test.hs。
这也发生在我身上——如果其他人碰到它并偶然发现这个页面,我的问题是我运行 GHCI 的虚拟机磁盘空间不足——提示它每次都尝试加载一个空文件。