Bri*_*Yeh 2 haskell class instance
我在名为Tree2.hs的文件中创建了一个Tree结构
module Tree2
(
Tree
) where
data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show)
Run Code Online (Sandbox Code Playgroud)
然后我导入它并尝试将其用作类的实例
import qualified Tree2
class YesNo a where
yesno :: a -> Bool
instance YesNo (Tree2.Tree a) where
yesno EmptyTree = False
yesno _ = True
Run Code Online (Sandbox Code Playgroud)
但是我在ghci中加载时遇到此错误:
Not in scope: data constructor ‘EmptyTree’
Failed, modules loaded: Tree2.
Run Code Online (Sandbox Code Playgroud)
谁知道为什么?
Dog*_*ert 11
第一,
module Tree2
(
Tree
) where
Run Code Online (Sandbox Code Playgroud)
只导出Tree数据类型,而不是其构造函数; 你应该使用
module Tree2
(
Tree(..)
) where
Run Code Online (Sandbox Code Playgroud)
代替.
其次,当您进行合格的导入时,您需要使用Tree2.EmptyTree而不是仅仅使用EmptyTree.
| 归档时间: |
|
| 查看次数: |
923 次 |
| 最近记录: |