我在名为 Tree2.hs 的文件中创建了一个树结构
module Tree2
(
Tree
) where
data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show)
然后我将其导入并尝试将其用作类的实例
import qualified Tree2
class YesNo a where
yesno :: a -> Bool
instance YesNo (Tree2.Tree a) where
yesno EmptyTree = False
yesno _ = True
但是我在 ghci 中加载它时遇到了这个错误:
Not in scope: data constructor ‘EmptyTree’
Failed, modules loaded: Tree2.
有人知道为什么吗?
最佳答案
首先,
module Tree2
(
Tree
) where
仅导出Tree
数据类型,不导出其构造函数;你应该使用
module Tree2
(
Tree(..)
) where
相反。
其次,当您进行合格导入时,您需要使用 Tree2.EmptyTree
而不仅仅是 EmptyTree
。
关于class - 导入 haskell 模块说 "not in scope",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30831956/