Can someone provide a complete hand calculation of zip [1,2,3] [4,5,6] using the following definition of zip, based on foldr:
zip :: [a] -> [b] -> [(a, b)]
zip = foldr f e
where
e ys = []
f x g [ ] = []
f x g (y : ys) = (x , y) : g ys
foldr :: (a -> b -> b) -> b -> ([a] -> b)
foldr _ e [] = e
foldr f e (x : xs) = f x (foldr f e xs)
This implementation of zip produces the expected result [(1, 4), (2, 5), (3, 6)], but I'm unable to do the hand calculation and don't understand why it works. Part of my problem is that "e" is defined as a function that takes one argument, I don't see how that fits in with the usual scheme for foldr, which, as I understand it, is:
foldr f e [x1, x2, ...] = f x1 (f x2 (f x3 ...(f xn e)))...
Thanks, as always, to all in this great community.
Windows Live™: Keep your life in sync. Check it out.