Learn You a Haskell explains zip4
:
--ghci> zip4 [2,3,3] [2,2,2] [5,5,3] [2,2,2]
-- [(2,2,5,2),(3,2,5,2),(3,2,3,2)]
Please critique my implementation:
zip4 :: [a] -> [a] -> [a] -> [a] -> [(a,a,a,a)]
zip4 xs ys zs as = zip4' xs ys zs as
where zip4' [] _ _ _ = []
zip4' _ [] _ _ = []
zip4' _ _ [] _ = []
zip4' _ _ _ [] = []
zip4' (b:bs) (c:cs) (d:ds) (e:es) = (b,c,d,e) : zip4' bs cs ds es
Tests:
*Main> zip4 [2,3,3] [2,2,2] [5,5,3] []
[]
*Main> zip4 [2,3,3] [2,2,2] [5,5,3] [1]
[(2,2,5,1)]
*Main> zip4 [2,3,3] [2,2,2] [5,5,3] [3]
[(2,2,5,3)]
*Main> zip4 [2,3,3] [2,2,2] [5,5,3] [3..100]
[(2,2,5,3),(3,2,5,4),(3,2,3,5)]
*Main> zip4 [1,1,1] [2,2,2] [3,3,3] (replicate 100 100)
[(1,2,3,100),(1,2,3,100),(1,2,3,100)]