zipWith
code:hs
zipWith :: (a -> b -> c) -> a -> b -> c zipWith _ [] _ = []
ziipWith _ _ [] = []
zipWith f (x:xs) (y:ys) = f x y : zipWith f xs ys
使用例
code:hs
code:ghci(hs)
f = fix $ \fibs -> 1 : 1 : zipWith (+) fibs (tail fibs)
二重リストの要素同士の積
code:hs
zipWith (zipWith (*)) 1, 2], [3, 4 5, 6], [7, 8
-- 結果: 5, 12], [21, 32
二重リストのzip
code:hs
zipWith zip 1, 2, 3], [4, 5, 6 7, 8, 9], [10, 11, 12
-- 結果: (1,7),(2,8),(3,9)],[(4,10),(5,11),(6,12)