I've troubles understanding the reasoning behind types signatures in Haskell.
1) as -> is said to be right associative, does it mean that it could be understood in the similar way as, for example 4^(2^(3^2))?
2) using type signature of simple function so as to express my doubts (to explain the way I understand it, I'll use a,b,c instead of Num a => a's or Int's):
myAdd :: a -> b -> c
myAdd x y = x+y
It means that function takes parameter a and returns function that takes b and finally returns c
But it could be re-written as:
myAdd :: (a->(b->c))
As most of the learning material states that c in our example is the result of function myAdd, why according to the use of brackets it indicates that first 'operation' is b->c? How do I deduce from that type signature the order of performed operations?
3) I was given a task to implement
map f xs
using foldr, (.), and (:) which resulted in:
map f xs = foldr ((:) . f) [] xs
I've no problems understanding the workings of the above functions, but here we come again - type signatures. If we assume, that names are unified so type a represents the same type in all contracts, it seems, that c and d can be expressed in terms of a and b. In mathematics, similar task would be probably quite straightforward, but how do I approach it in Haskell?
map :: (a -> b) -> [a] -> [b]
foldr :: (a -> c -> c) -> c -> [a] -> c
(:) :: b -> ([b] -> [b])
(.) :: (b -> d) -> (a -> b) -> a -> d
Using your notation, in
myAdd :: a -> b -> c
myAdd x y = x+y
you correctly interpret the type as a -> (b->c) but then you go on to suggest that the computation in b -> c is somehow done first.
When something like myAdd 2 10 is evaluated, the function evaluation is left to right.
1) First myAdd 2 is evaluated. The result of this evaluation is the function which sends a given number y to 2 + y. In effect, the definition of myAdd is the same as
myAdd x = \y -> x+y
2) This last function is then applied to the argument 10 to yield 2 + 10 = 12
Thus, the right associativity of -> in the type expression doesn't correspond to a right to left ordering of computation in the function evaluation. In fact, function evaluation is left-associative: myAdd 2 10 is the same as (myAdd 2) 10.
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