Haskell 不在范围内的数据构造函数
我有两个.hs文件:一个包含新的类型声明,另一个使用它 第一.hs:Haskell 不在范围内的数据构造函数,haskell,Haskell,我有两个.hs文件:一个包含新的类型声明,另一个使用它 第一.hs: module first () where type S = SetType data SetType = S[Integer] 第二点: module second () where import first 当我运行second.hs时,两个模块first和second都加载得很好 但是,当我在Haskell平台上编写:typeS时,出现以下错误 不在范围内:数据构造函数的 注意:每个模块
module first () where
type S = SetType
data SetType = S[Integer]
第二点:
module second () where
import first
当我运行second.hs时,两个模块first和second都加载得很好
但是,当我在Haskell平台上编写:type
S时,出现以下错误
不在范围内:数据构造函数的
注意:每个模块中肯定都有一些函数,为了简洁起见,我跳过它
假设实际上模块名必须以大写字母开头,空的导出列表-()
-表示模块不导出任何内容,因此第一个中定义的内容不在第二个中的范围内
完全忽略导出列表以导出所有顶级绑定,或在导出列表中列出导出的实体
module First (S, SetType(..)) where
((..)
还导出SetType
的构造函数,如果没有该构造函数,则只导出类型)
并用作
module Second where
import First
foo :: SetType
foo = S [1 .. 10]
或者,将导入限制为特定类型和构造函数:
module Second where
import First (S, SetType(..))
您还可以缩进顶层
module Second where
import First
foo :: SetType
foo = S [1 .. 10]
但这很难看,而且很容易因为计算错误而出错。
- 模块名称以大写字母开头-Haskell区分大小写
- 在左边空白处排列代码-布局在Haskell中很重要
- 括号中的位是导出列表-如果要导出所有函数,或将所有要导出的内容都放在其中,请忽略它
First.hs
:
module First where
type S = SetType
data SetType = S[Integer]
module Second where
import First
Second.hs
:
module First where
type S = SetType
data SetType = S[Integer]
module Second where
import First
是的,它以大写字母开头(我只是忘了在这里这样写)那么在哪里写导入行?是的,否则就不会编译了。在哪里写导入第一行,这样它的数据类型就在第二行的范围内?导入直接在模块声明之后。用于它们的缩进决定了模块的缩进,通常情况下,顶层不使用缩进。所以模块第二个,其中\n\n导入第一个\n\n…
。(\n
应为实际换行)