在test.hs中,我有:
doubleMe x = x + x
Run Code Online (Sandbox Code Playgroud)
在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>
Run Code Online (Sandbox Code Playgroud)
为什么?怎么修?
jkr*_*mer 29
我的猜测是你在源文件中定义了一个main函数.
如果已定义main函数,则加载模块:l test将不会导入任何函数main.在这种情况下,您可以通过在模块名称前添加一个asterix来加载它::l *test.原因是编译后的二进制文件将隐藏非导出的顶级函数.前缀asterix强制GHCi忽略预编译模块(测试)并解释源文件(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
Run Code Online (Sandbox Code Playgroud)
查看这些链接以获取更多信息:
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的作用域
从目录中删除test.hi和test.o然后尝试ghci test.[有时当我运行ghc file.hs(而不是ghc --make file.hs)它会给出未定义的引用错误,但会创建ghci稍后读取的文件.也许这是一个错误.]
尝试
:cd "<path to your file>"
:l test
:browse
Run Code Online (Sandbox Code Playgroud)
在ghci.结果是什么?
您确定正在加载正确的 test.hs 吗?也许您位于错误的目录中。或者也许您在添加 doubleMe 的定义后没有保存 test.hs 。
| 归档时间: |
|
| 查看次数: |
5903 次 |
| 最近记录: |