带Int的模糊类型变量

时间:2019-04-21 18:23:41

标签: haskell

我正在尝试从“为您带来美好的Haskell”中教自己Haskell! On Page 143 there's a section on creating a function that evaluates like a Javascript If statement e.g. returning false for values like "" and 0.因此,我复制了下面的代码,然后运行它,它适用于除整数以外的所有类型的数据。当我键入“ yesno 0”时,我收到有关歧义类型变量的错误。看来Int类型的模式匹配失败,但是我很确定我已经从书中正确地复制了该模式。我该如何解决?

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

0 个答案:

没有答案