What does the => symbol mean in Haskell?

后端 未结 4 1954
礼貌的吻别
礼貌的吻别 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:22

    One way to think about it is that Ord a and Num a are additional inputs to the function. They are a special kind of input though: dictionaries. When you use this function with a particular type a, there must also be dictionaries available for the Ord and Num operations on the type a as well.

    Any function that makes use of a function with dictionary inputs must also have the same dictionary inputs.

    foo :: (Num a, Ord a) => a -> t
    foo x = loop x someFunc someT
    

    However, you do not have to explicitly pass these dictionaries around. Haskell will take care of that for you, assuming there is a dictionary available. You can create a dictionary with a typeclass instance.

    instance Num MyType with
      x + y = ...
      x - y = ...
      ...
    

    This creates a dictionary for the Num operations on MyType, therefore MyType can be used anywhere that Num a is a required input (assuming it satisfies the other requirements, of course).

提交回复
热议问题