[Haskell-beginners] Type class instance with Num

Galaxy Being borgauf at gmail.com
Sun Apr 4 04:26:09 UTC 2021


I'm following LYHFGG and I have this

class YesNo a where
    yesno :: a -> Bool

instance YesNo Int where
    yesno 0 = False
    yesno _ = True

but then I have to specify Int here

> yesno (5 :: Int)
True

Just with 5 gives this error

Ambiguous type variable ‘a0’ arising from the literal ‘5’
      prevents the constraint ‘(Num a0)’ from being solved.
      Probable fix: use a type annotation to specify what ‘a0’ should be.

I tried this

instance YesNo (Num a) where
    yesno 0 = False
    yesno _ = True

but got cryptic errors. What can I do to make yesno take any of Num's
numbers?

LB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20210403/8c2f8a3d/attachment.html>


More information about the Beginners mailing list