在加载源文件时,它指出使用-i选项指定用于查找源文件的搜索路径:
ghci -idir1:...:dirn
Run Code Online (Sandbox Code Playgroud)
这是否意味着当一个人执行时:
:load test.hs
Run Code Online (Sandbox Code Playgroud)
然后ghci在上面的目录中查找test.hs?我在问题指定源目录到GHC时看到了响应, 但我仍然不清楚这一点.
例如,在Windows XP中,我将test.hs放入:
C:\Documents and Settings\winuser\My Documents
Run Code Online (Sandbox Code Playgroud)
然后跑:
ghci -iC:\Documents and Settings\winuser\My Documents
Run Code Online (Sandbox Code Playgroud)
但是,在做的时候:load test.hs,ghci抱怨无法找到该文件.
[编辑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
Run Code Online (Sandbox Code Playgroud)
然后我可以做以下事情:
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
Run Code Online (Sandbox Code Playgroud)
但是,当模块存储在不同位置的文件中时,我还没有找到使模块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'
Run Code Online (Sandbox Code Playgroud)
[编辑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.
Run Code Online (Sandbox Code Playgroud)
加载路径是GHCi搜索模块的方式.所以如果你命名你的模块Test.hs并添加
module Test where
Run Code Online (Sandbox Code Playgroud)
比你能做的
> :load Test
Run Code Online (Sandbox Code Playgroud)
否则你可以使用
> :cd SomeDirectory
> :load test.hs
Run Code Online (Sandbox Code Playgroud)
回复编辑:
(警告,我运行eshell,因此命令/路径看起来不同)
~ $ mkdir temp
~ $ mkdir temp/temp temp/temp2
temp $ find-file temp/A.hs
-- In A.hs
module A where
addA = (+)
--
temp $ find-file temp2/B.hs
-- In B.hs
module B where
addB = (+)
--
temp $ cd temp
temp/temp $ ghci -i../temp2
> :load A B
> import B
Run Code Online (Sandbox Code Playgroud)
现在我可以访问A和B.