Haskell中的zipWith函数

Cat*_*yLu 7 haskell

ghci> zipWith' (zipWith' (*)) [[1,2,3],[3,5,6],[2,3,4]] [[3,2,2],[3,4,5],[5,4,3]]
Run Code Online (Sandbox Code Playgroud)

函数zipWith'使用函数'*'和它之后的参数得到返回.但在这种情况下,函数zipWith如何得到结果[[3,4,6],[9,20,30],[ [10,12,12]].

使用zipWith'的代码示例是从免费在线书籍Learn You a Haskell for Great Good中逐字逐句获取的.

sep*_*p2k 22

zipWith在两个列表的每个成员上成对调用给定的函数.所以zipWith f [a,b,c] [x,y,z]评估为[f a x, f b y, f c z].在这种情况下fzipWith (*)与列表的元素再次列出,让您得到:

[ zipWith (*) [1,2,3] [3,2,2],
  zipWith (*) [3,5,6] [3,4,5], 
  zipWith (*) [2,3,4] [5,4,3] ]
Run Code Online (Sandbox Code Playgroud)

现在内部调用zipWith成对地增加内部列表的元素,所以你得到:

[ [3,4,6],
  [9,20,30],
  [10,12,12] ]
Run Code Online (Sandbox Code Playgroud)