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)
(->) 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.
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.
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.
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With