zipWith function in Haskell
ghci> zipWith' (zipWith' (*)) [[1,2,3],[3,5,6],[2,3,4]] [[3,2,2],[3,4,5],[5,4,3]]
开发者_运维知识库The function zipWith' use function '*' and parameters after it to get the return.But in this case,how the function zipWith' to get the result [[3,4,6],[9,20,30],[10,12,12]] .
The code example using zipWith' was taken verbatim from the free online book Learn You a Haskell for Great Good.
zipWith
calls the given function pairwise on each member of both lists. So zipWith f [a,b,c] [x,y,z]
evaluates to [f a x, f b y, f c z]
. In this case f
is zipWith (*)
and the elements of the lists are again lists, so you get:
[ zipWith (*) [1,2,3] [3,2,2],
zipWith (*) [3,5,6] [3,4,5],
zipWith (*) [2,3,4] [5,4,3] ]
Now the inner calls to zipWith
multiply the elements of the inner lists pairwise, so you get:
[ [3,4,6],
[9,20,30],
[10,12,12] ]
精彩评论