问题描述
在它表示用-i选项指定查找源文件的搜索路径:
ghci -idir1 :. ..:dirn
这是否意味着当您执行以下操作时:
:load test.hs
然后ghci在上面的test.hs目录中查找?我在
时看到响应,但我仍然不清楚这一点。
例如在Windows XP中,我将test.hs放入:
C:\\ \\ Documents and Settings \winuser\My Documents
然后运行:
ghci -iC:\文档和设置\winuser\我的文档
然而,在执行:load test.hs
时,ghci抱怨无法找到该文件。
我想避免使用:cd
因为它卸载了所有已加载的模块,这阻止了我从多个位置加载文件。
- C:\A\A.hs
模块A其中
myaddA :: Int-> Int-> Int
myaddA xy = x + y
--C:\ B\B.hs
module B where
myaddB :: Int-> Int-> Int
myaddB xy = x + y
然后我可以执行以下操作:
Prelude> :cd C:\A
Prelude> :加载A
[1之1]编译A(A.hs,解释)
确定,加载模块:A.
* A> myaddA 2 3
5
* A> :cd C:\ B
警告:更改目录会导致所有已加载的模块被卸载,
因为搜索路径已更改。
前奏> :加载B
[1之1]编译B(B.hs,解释)
好的,加载的模块:B.
* B> myaddB 3 4
7
然而,我还没有找到一种方法来创建模块A和B同时可用,当模块存储在不同位置的文件中时b
$ b
> ls
temp temp2
>更多temp / A.hs
模块A其中
addA =(+)
>更多temp2 / B.hs
模块B其中
addB =(+)
> cd temp
> ghci -i ../ temp2
GHCi,版本7.6.3:http://www.haskell.org/ghc/:?寻求帮助
加载包ghc-prim ...链接...完成。
加载包integer-gmp ...链接...完成。
加载程序包库...链接...完成。
前奏>导入AB
<互动>:1:10:输入'B'的解析错误
> ls
temp temp2
> more temp / A.hs
module A其中
addA =(+)
> more temp2 / B.hs
模块B其中
addB =( +)
> cd temp
> ghci -i ../ temp2
GHCi,版本7.6.3:http://www.haskell.org/ghc/:?寻求帮助
加载包ghc-prim ...链接...完成。
加载包integer-gmp ...链接...完成。
加载程序包库...链接...完成。
前奏>导入A
<无位置信息>:
无法找到模块A
它不是当前程序或任何已知软件包中的模块。
前奏>导入B
<无位置信息>:
找不到模块'B'
它不是当前程序或任何已知软件包中的模块。
加载路径是GHCi如何搜索模块。所以如果你命名你的模块 Test.hs
并添加了
module Test其中
比你可以做的更多
> :load test
否则您可以使用
> :cd SomeDirectory
> :load test.hs
编辑响应:
(警告:我运行了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
- 在B.hs
模块B中
addB =(+)
-
temp $ cd temp
temp / temp $ ghci -i ../ temp2
> :加载A B
>导入B
现在我可以访问 A
和 B
。
In Loading source files it states that the search path for finding source files is specified with the -i option :
ghci -idir1:...:dirn
Does this mean that when one performs :
:load test.hs
then ghci looks in the directories above for test.hs? I saw the response atProblem Specifying Source Directory to GHC but I am still not clear about this.
For example in Windows XP I put test.hs in :
C:\Documents and Settings\winuser\My Documents
and then ran :
ghci -iC:\Documents and Settings\winuser\My Documents
However upon doing :load test.hs
, ghci complained about not being able to find the file.
[EDIT 1]
I want to avoid using :cd
because it unloads all loaded modules, which prevents me from loading files from multiple locations
[EDIT 2 : response to 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
Then I can do the following :
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
However I haven't found a way to make modules A and B simultaneously available when the modules are stored in files in different locations
[EDIT 3 : response to 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'
[EDIT 4 : response to 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.
The load path is how GHCi searches for modules. So if you named your module Test.hs
and added
module Test where
Than you can do
> :load Test
otherwise you can use
> :cd SomeDirectory
> :load test.hs
Response to edit:
(Warning, I run eshell so the commands/paths look different)
~ $ 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
And now I have access to both A
and B
.
这篇关于为“加载”指定搜索路径在ghci中的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!