Haskell 中的多个源文件

发布于 2024-09-15 02:46:55 字数 733 浏览 4 评论 0原文

我正在用 Haskell 编写我的第一个大项目,我想将其拆分为多个文件。到目前为止,我已经编写了两个模块,ParseEval。我想要一个仅包含这两个模块并指定 main 函数的 Main 模块。我有文件 Main.hsParse.hsEval.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橪书 2024-09-22 02:46:55

来自 Haskell 报告

模块实现只能
导出其声明的实体,或
它从其他一些进口
模块。如果省略导出列表,
定义的所有值、类型和类
模块中的内容被导出,但不是
那些进口的

您需要在 Parse.hs 中提供包含 parse 的显式导出列表,或者在 Main.hs 中再次导入 parse

From the Haskell report:

A module implementation may only
export an entity that it declares, or
that it imports from some other
module. If the export list is omitted,
all values, types and classes defined
in the module are exported, but not
those that are imported
.

You either need to give an explicit export list that includes parse in Parse.hs, or import parse again in your Main.hs.

滴情不沾 2024-09-22 02:46:55

你也可以这样做:

module Parse (parse) where
    import qualified Text.ParserCombinators.Parsec as P

    parse = P.parse

但实际上,这是没有用的。在从模块之一导出它之前,您肯定会希望在 Parsec 之上构建一些更特定于域的东西。

You can also do this:

module Parse (parse) where
    import qualified Text.ParserCombinators.Parsec as P

    parse = P.parse

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文