要使这些类声明起作用,我需要哪种数据类型?
class YesNo a where
yesno :: a -> Bool
instance YesNo Int where
yesno 0 = False
yesno _ = True
instance YesNo [a] where
yesno [] = False
yesno _ = True
instance YesNo Bool where
yesno = id
instance YesNo (Maybe a) where
yesno (Just _) = True
yesno Nothing = False
目前*Main> :t yesno
产生<interactive>:1:1: error: Variable not in scope: yesno
,大概是因为我需要定义适当的数据类型。
建议我将约束(YesNo a) =>
添加到yesno
的类型似乎不起作用(在询问<interactive>:14:1: error: Variable not in scope
类型时我仍然得到yesno
:
*Main> :l experiment
[1 of 3] Compiling Geometry ( Geometry.hs, interpreted )
[2 of 3] Compiling Shapes ( Shapes.hs, interpreted )
[3 of 3] Compiling Main ( experiment.hs, interpreted )
Ok, three modules loaded.
*Main> :t yesno
<interactive>:1:1: error: Variable not in scope: yesno