Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Haskell use -> instead of =?

Why does Haskell use "->" where it seemingly could have just used "="?

For example, what's wrong with this?

take m ys               = case (m,ys) of
                            (0,_)       =  []
                            (_,[])      =  []
                            (n,x:xs)    =  x : take (n-1) xs

or

(\x = x * x)
like image 807
BasilTomato Avatar asked Aug 07 '14 13:08

BasilTomato


People also ask

What does -> mean in Haskell?

(->) is often called the "function arrow" or "function type constructor", and while it does have some special syntax, there's not that much special about it. It's essentially an infix type operator. Give it two types, and it gives you the type of functions between those types.

What does <$> mean in Haskell?

It's merely an infix synonym for fmap , so you can write e.g. Prelude> (*2) <$> [1.. 3] [2,4,6] Prelude> show <$> Just 11 Just "11" Like most infix functions, it is not built-in syntax, just a function definition. But functors are such a fundamental tool that <$> is found pretty much everywhere.

What does the dollar symbol mean in Haskell?

The dollar sign, $ , is a controversial little Haskell operator. Semantically, it doesn't mean much, and its type signature doesn't give you a hint of why it should be used as often as it is. It is best understood not via its type but via its precedence.


1 Answers

It would be unfortunate to write

(0, _) = []

because that is not true.

In the tradition of Robert Recorde, we try to write equations only when we intend the left-hand side to equal the right-hand side. So we write

dup x = (x, x)

to make dup x equal to (x, x), or

dup = \ x -> (x, x)

to make dup equal to the function which maps x to (x, x), but not

\ x = (x, x)

because there is no way to make x equal (x, x).

We depart from the tradition only slightly when we allow "falling through", e.g.,

f 0 = 1
f n = 2 * f (n - 1)

but only in the sense that the second line has a silent "otherwise".

like image 138
pigworker Avatar answered Oct 12 '22 13:10

pigworker