Haskell zipWith function

ghci> zipWith' (zipWith' (*)) [[1,2,3],[3,5,6],[2,3,4]] [[3,2,2],[3,4,5],[5,4,3]] 

The zipWith 'function uses the' * 'function and the parameters after it to get return.But in this case, like the zipWith' function to get the result [[3,4,6], [9,20,30], [10, 12 , 12]].

Sample code using zipWith 'was verbatim from the free online book Learn You About Haskell for Great Good .

+4
source share
1 answer

zipWith calls this function in pairs in each member of both lists. Therefore, zipWith f [a,b,c] [x,y,z] is evaluated as [fax, fby, fcz] . In this case, f is zipWith (*) , and the list items 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 zipWith internal calls replenish the elements of internal lists in pairs, so you get:

 [ [3,4,6], [9,20,30], [10,12,12] ] 
+16
source

Source: https://habr.com/ru/post/1336256/


All Articles