Skip to content

Instantly share code, notes, and snippets.

@dtchepak
Created February 29, 2012 12:27
Show Gist options
  • Save dtchepak/1940471 to your computer and use it in GitHub Desktop.
Save dtchepak/1940471 to your computer and use it in GitHub Desktop.
Trying to implement zip using foldr
zip' :: [a] -> [b] -> [(a,b)]
zip' x y = foldr (\h a ->
foldr (\h2 _ -> ((h,h2):a)) [] y
) [] x
{-
*Main> zip' [] []
[]
*Main> zip' [1] [2]
[(1,2)]
*Main> zip' [1] [3]
[(1,3)]
*Main> zip' [1,5] [10,20]
[(1,10),(5,10)]
*Main> zip' [1..5] [10..20]
[(1,10),(2,10),(3,10),(4,10),(5,10)]
*Main> zip' [1..10] [10..15]
[(1,10),(2,10),(3,10),(4,10),(5,10),(6,10),(7,10),(8,10),(9,10),(10,10)]
*Main> take 5 (zip ['a'..'f'] [1..])
[('a',1),('b',2),('c',3),('d',4),('e',5)]
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment