Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a boolean in Haskell

Tags:

haskell

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.

like image 310
lfnunley Avatar asked Mar 23 '13 06:03

lfnunley


1 Answers

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.

like image 171
Neil Forrester Avatar answered Dec 28 '22 23:12

Neil Forrester