Haskell 中的多个源文件
我正在用 Haskell 编写我的第一个大项目,我想将其拆分为多个文件。到目前为止,我已经编写了两个模块,Parse
和 Eval
。我想要一个仅包含这两个模块并指定 main 函数的 Main
模块。我有文件 Main.hs
、Parse.hs
和 Eval.hs
并将它们导入到 Main
中,但会发生这种情况:
Prelude> :load "~/code/haskell/lisp/Main.hs"
[1 of 3] Compiling Eval ( Eval.hs, interpreted )
[2 of 3] Compiling Parse ( Parse.hs, interpreted )
[3 of 3] Compiling Main ( ~/code/haskell/lisp/Main.hs, interpreted )
Ok, modules loaded: Main, Parse, Eval.
*Main> parse parseExpr "" "#b101"
<interactive>:1:0: Not in scope: `parse'
parse
函数来自 Parsec 库,该库在 Parse.hs
中导入。怎么了?
I'm writing my first big project in Haskell and I'd like to split it across multiple files. So far, I have written two modules, Parse
and Eval
. I'd like to have a Main
module that just includes these two modules and specifies the main
function. I have the files Main.hs
, Parse.hs
, and Eval.hs
and import them in Main
, but this happens:
Prelude> :load "~/code/haskell/lisp/Main.hs"
[1 of 3] Compiling Eval ( Eval.hs, interpreted )
[2 of 3] Compiling Parse ( Parse.hs, interpreted )
[3 of 3] Compiling Main ( ~/code/haskell/lisp/Main.hs, interpreted )
Ok, modules loaded: Main, Parse, Eval.
*Main> parse parseExpr "" "#b101"
<interactive>:1:0: Not in scope: `parse'
The parse
function comes from Parsec library, which is imported in Parse.hs
. What's wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 Haskell 报告:
您需要在
Parse.hs
中提供包含parse
的显式导出列表,或者在Main.hs 中再次导入
。parse
From the Haskell report:
You either need to give an explicit export list that includes
parse
inParse.hs
, or importparse
again in yourMain.hs
.你也可以这样做:
但实际上,这是没有用的。在从模块之一导出它之前,您肯定会希望在 Parsec 之上构建一些更特定于域的东西。
You can also do this:
But really, this is useless. You'll certainly be wanting to build something more domain-specific on top of Parsec before you export it from one of your modules.