In the book titled "Programming in Haskell", on page 77, there is an implementation of foldr
, in order to explain the function. It looks like this:
foldr :: (a -> b -> b) -> b -> [a] -> b
foldr f v [] = v
foldr f v (x:xs) = f x (foldr f v xs)
Why isn't the type more like this:
foldr :: (a -> a -> b) -> a -> [a] -> b
The first argument, which is a function (a -> b -> b), will always be applied to the head of the list, and the recursively processed tail.
But what would be an example, where the head and the recursively processed tail end up having different types?
I surely don't understand something here. Could you break down the process of writing the type for this implementation of foldr?