为什么我不能导入这个Haskell模块?

sta*_*ser 2 haskell

我把它放进去~/Desktop/Shapes.hs:

module Shapes   
( Shape(Rectangle)
) where 

data Shape = Circle | Rectangle deriving (Show)
Run Code Online (Sandbox Code Playgroud)

然后我这样做:

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.
Run Code Online (Sandbox Code Playgroud)

为什么我会收到此错误?

我也试过先编译ghc -c Shapes.hs.它仍然无法正常工作.

我在我的OS X 10.9.2 Mavericks上从haskell.org安装了"Haskell Platform 2013.2.0.0 for Mac OS X,64 bit".我也按照他们的ghc-clang-wrapper指示行事.

更新:

有人建议先做:l Shapes.hs.问题是:l Shapes.hs加载整个Shapes文件,这意味着我可以访问Circle值构造函数,即使我没有导出它.请参阅我之前的问题:为什么我可以使用这个"私有"值构造函数?我只想加载模块.这可能吗?

Lee*_*hem 6

你需要加载你的Shapes.hs第一个:l Shapes.hs.

  1. 因为你Shapes没有加载,所以:m Shapes不会工作.

  2. 因为您Shapesghci可以查找的已编译包中不存在,所以import Shapes不起作用.

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

请参阅:
提示符范围内的实际内容是什么?
:module和:load

  • @stackoverflowuser首先`:加载Shapes.hs`,然后加载`:module Shapes`. (2认同)