Haskell:加载当前目录路径中的所有文件



命令(在GHCi中)

:load abc

加载文件 abc 中的函数(必须存在于当前目录路径中)。如何加载当前目录路径中的所有文件?谢谢

----------------------------------------------------------------------------------

[对以下帖子的回应]

嗨,Rotskoff,谢谢我尝试了你的建议,但我无法让它工作,所以我想我一定误解了什么。

我创建了 3 个文件 test.hs、test1.hs 和 test2.hs,如下所示:

->

--test.hs
import NecessaryModule

->

--test1.hs
module NecessaryModule where
addNumber1 :: Int -> Int -> Int
addNumber1 a b = a + b

->

--test2.hs
module NecessaryModule where
addNumber2 :: Int -> Int -> Int
addNumber2 a b = a + b

然后当我这样做时:

:load test

我收到错误消息:

test.hs:1:8:
    Could not find module `NecessaryModule':
      Use -v to see a list of the files searched for.

谢谢

---------------------------------------------------------------------------------

谢谢。这就是我为了让它工作所做的(遵循 Rotskoff 的建议):

->

--test.hs
import NecessaryModule1
import NecessaryModule2

->

--NecessaryModule1.hs
addNumber1 :: Int -> Int -> Int
addNumber1 a b = a + b

->

--NecessaryModule2.hs
addNumber2 :: Int -> Int -> Int 
addNumber2 a b = a + b

大概你的意思是Haskell源文件,因为你不能将GHCi中的:load命令用于其他任何事情。

在加载的源文件的顶部,包含以下行:

import NecessaryModule

对于每个源文件,请确保命名模块,例如,

module NecessaryModule where

应该出现。GHCi将自动链接所有文件。

如果您尝试导入数据,请查看文档中的System.Directory

文件名

和模块名称最好相同:

➤ mv test1.hs NecessaryModule.hs
➤ ghci
GHCi, version 7.0.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Prelude> :load test
[1 of 2] Compiling NecessaryModule  ( NecessaryModule.hs, interpreted )
[2 of 2] Compiling Main             ( test.hs, interpreted )
Ok, modules loaded: NecessaryModule, Main.

因为:load命令加载模块(按文件名)及其依赖项(您可以通过在 GHCi 提示符中键入 :help:? 来阅读)。

此外,:load命令会擦除当前 GHCi 会话中定义的所有先前声明,因此要加载当前目录中的所有文件,您可以执行以下操作:

Prelude> :q
Leaving GHCi.
➤ ghci *.hs
GHCi, version 7.0.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
<no location info>:
    module `main:NecessaryModule' is defined in multiple files: NecessaryModule.hs
                                                            test2.hs
Failed, modules loaded: none.
Prelude> :q
Leaving GHCi.
➤ rm test2.hs
➤ ghci *.hs  
GHCi, version 7.0.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
[1 of 2] Compiling NecessaryModule  ( NecessaryModule.hs, interpreted )
[2 of 2] Compiling Main             ( test.hs, interpreted )
Ok, modules loaded: NecessaryModule, Main.
*NecessaryModule> 

最新更新