I am playing a bit with zipWith and encounter following:
Prelude Control.Applicative> :t zipWith id
zipWith id :: [b -> c] -> [b] -> [c]
Why does the compiler expect for the next argument a list of functions?
I tried to analyze, but could not conclude, why the next argument must be a list of functions.
How did the signature is getting apply, when I pass id to zipWith?
The type of
zipWithis:And the type of
idis:So if we now want to derive the type of
zipWith id, we push the type ofid :: d -> dinto the type of the first argument ofzipWith:So that means that:
a ~ danda ~ b -> c. So that means that the type ofzipWith idis now:How does this work: the first list has to contain a list of functions
f :: b -> c, and the second list, a list of elementsx :: b, and it thus calculates a list of elementsf x :: c.For example:
since
1+1is2.0,5/4is1.25,3*2is6.0and3-5is-2.0.So
zipWith idwill take two elementsfandx, and applyid f xon these, or more verbose(id f) x. Sinceid fisf, it will thus calculatef x.We can thus conclude that
zipWithis an elementwise mapping.