What does the => symbol mean in Haskell?

后端 未结 4 1978
礼貌的吻别
礼貌的吻别 2020-12-13 06:54

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 =>

4条回答
  •  长情又很酷
    2020-12-13 07:03

    This is a typeclass constraint; (Num a, Ord a) => ... means that loop works with any type a that is an instance of the Num and Ord typeclasses, corresponding to numeric types and ordered types respectively. Basically, you can think of loop as having the type on the right hand side of the =>, except that a is required to be an instance of Num and Ord.

    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, Num includes numeric operations like addition and multiplication, while Ord includes less than, greater than, and so on.

    For more information on typeclasses, see this introduction from Learn You a Haskell.

提交回复
热议问题