gpt4 book ai didi

haskell - 为什么我无法导入这个 Haskell 模块?

转载 作者:行者123 更新时间:2023-12-02 14:35:22 25 4
gpt4 key购买 nike

我把它放在~/Desktop/Shapes.hs中:

module Shapes   
( Shape(Rectangle)
) where

data Shape = Circle | Rectangle deriving (Show)

然后我这样做:

cd ~/Desktop
ghci

ghci> :m +Shapes

<no location info>:
Could not find module `Shapes'
It is not a module in the current program, or in any known package.

ghci> import Shapes

<no location info>:
Could not find module `Shapes'
It is not a module in the current program, or in any known package.

为什么我会收到此错误?

我还尝试首先使用 ghc -c Shapes.hs 进行编译。还是不行。

我在我的 OS X 10.9.2 Mavericks 上安装了来自 haskell.org 的“Haskell Platform 2013.2.0.0 for Mac OS X,64 位”。我还遵循了他们的 ghc-clang-wrapper 说明。

更新:

有人建议先执行 :l Shapes.hs 。问题是 :l Shapes.hs 加载整个 Shapes 文件,这意味着我可以访问 Circle 值构造函数,即使我没有导出它。请参阅我之前的问题:Why can I use this "private" value constructor?我想仅加载模块。这可能吗?

最佳答案

您需要先通过 :l Shapes.hs 加载 Shapes.hs

  1. 因为您的Shapes尚未加载,所以:m Shapes将不起作用。

  2. 因为您的Shapes不存在于ghci可以找到的编译包中,所以导入Shapes将不起作用。

如果您只想在范围内导出符号,则在 :load 模块之后,您可以使用 :moduleimport 仅导入那些符号。例如,在 :load Shapes.hs:module Shapes 之后,Rectangle 将在范围内,但 Circle 不会.

参见:
What's really in scope at the prompt?
:module and :load

关于haskell - 为什么我无法导入这个 Haskell 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23428466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com