I have been learning Haskell using learnyouahaskell, and I have run into a problem on a program I've been attempting. I want to evaluate three Num inputs representing sides of a triangle, and see if they represent a right triangle. Here is what I have so far:
isRight :: (Num a) => a -> a -> a -> Bool
isRight x y z = (x^2 + y^2) == z^2
Obviously the function will need to be longer to allow for different orders of sides, but for now I'm unable to use the function due to this error in GCHI:
Could not deduce (Eq a) arising from use of '==' from the context (Num a)
bound by the type signature for
isRight :: Num a => a -> a -> a -> Bool
at isRight.hs:2:1-34
Obviously I do not understand how to return a boolean type in Haskell, and I have been unable to find any help pertaining to this problem online. I would be grateful if somebody would help to explain this to me.
Your problem is not with returning Bool. The trouble is that not all members of the Num
typeclass are also members of the Eq
typeclass. This will fix your code.
isRight :: (Num a, Eq a) => a -> a -> a -> Bool
isRight x y z = (x^2 + y^2) == z^2
You can read more about typeclasses from the relevant section of the very book you're reading: Learn You a Haskell.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With