In 加载源文件 http://www.haskell.org/ghc/docs/7.6.1/html/users_guide/loading-source-files.html它指出查找源文件的搜索路径是使用 -i 选项指定的:
ghci -idir1:...:dirn
这是否意味着当一个人执行时:
:load test.hs
然后 ghci 在上面的目录中查找 test.hs?我看到了回复为 GHC 指定源目录时出现问题 https://stackoverflow.com/a/5760396但我对此还不清楚。
例如,在 Windows XP 中,我将 test.hs 放入:
C:\Documents and Settings\winuser\My Documents
然后跑:
ghci -iC:\Documents and Settings\winuser\My Documents
然而在做之后:load test.hs
,ghci 抱怨无法找到该文件。
[EDIT 1]
我想避免使用:cd
因为它会卸载所有已加载的模块,这会阻止我从多个位置加载文件
[编辑2:对jozefg的回应]
--C:\A\A.hs
module A where
myaddA::Int->Int->Int
myaddA x y = x+y
--C:\B\B.hs
module B where
myaddB::Int->Int->Int
myaddB x y = x+y
然后我可以执行以下操作:
Prelude> :cd C:\A
Prelude> :load A
[1 of 1] Compiling A ( A.hs, interpreted )
Ok, modules loaded: A.
*A> myaddA 2 3
5
*A> :cd C:\B
Warning: changing directory causes all loaded modules to be unloaded,
because the search path has changed.
Prelude> :load B
[1 of 1] Compiling B ( B.hs, interpreted )
Ok, modules loaded: B.
*B> myaddB 3 4
7
但是,当模块存储在不同位置的文件中时,我还没有找到使模块 A 和 B 同时可用的方法
[编辑3:对jozefg的回应]
>ls
temp temp2
>more temp/A.hs
module A where
addA = (+)
>more temp2/B.hs
module B where
addB = (+)
>cd temp
>ghci -i../temp2
GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> import A B
<interactive>:1:10: parse error on input `B'
[编辑4:对jozefg的回应]
>ls
temp temp2
>more temp/A.hs
module A where
addA = (+)
>more temp2/B.hs
module B where
addB = (+)
>cd temp
>ghci -i../temp2
GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> import A
<no location info>:
Could not find module `A'
It is not a module in the current program, or in any known package.
Prelude> import B
<no location info>:
Could not find module `B'
It is not a module in the current program, or in any known package.