Reputation: 83
If I have these functions:
add :: Int -> Int -> Int
add a b = a+b
map :: (a->b) -> [a] -> [b]
map f [] = []
map f (x:xs) = f x : map f xs
When I call map (add 1) [1,2,3]
the output is [2,3,4]
but how does the evaluation of Haskell work in this case?
My ideas:
It matches with the second pattern because map is a function and [1,2,3] a list.
Then we would have:
map (add 1) [1,2,3]
= add 1 : map add [2,3]
= add 1 : add 2 : map add [3]
= add 1 : add 2 : add 3 : map add []
= add 1 : add 2 : add 3 : []
However this cannot be true. Can someone help me please?
Upvotes: 1
Views: 468
Reputation: 1119
add 1
is the unary function f
, not add
. So you end up with the following.
map (add 1) [1,2,3]
== add 1 1 : map (add 1) [2,3]
== add 1 1 : add 1 2 : map (add 1) [3]
== add 1 1 : add 1 2 : add 1 3 : map (add 1) []
== 2 : 3 : 4 : []
== [2,3,4]
Upvotes: 2