I’m new to Haskell and, in general, to functional programming, and I’m a bit uncomfortable with its syntax.
In the following code what does the => denote? And also (Num a, Ord a)?
loop :: (Num a, Ord a) => a -> (t -> t) -> t -> t
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is a typeclass constraint;
(Num a, Ord a) => ...means thatloopworks with any typeathat is an instance of theNumandOrdtypeclasses, corresponding to numeric types and ordered types respectively. Basically, you can think ofloopas having the type on the right hand side of the=>, except thatais required to be an instance ofNumandOrd.You can think of typeclasses as basically similar to OOP interfaces (but they’re not the same thing!) — they encapsulate a set of definitions which any instance must support, and generic code can be written using these definitions. For instance,
Numincludes numeric operations like addition and multiplication, whileOrdincludes less than, greater than, and so on.For more information on typeclasses, see this introduction from Learn You a Haskell.