[Haskell-cafe] Hand calculation of Bird's definition of zip using foldr

R J rj248842 at hotmail.com
Thu Mar 12 13:01:02 EDT 2009


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™: Life without walls.
http://windowslive.com/explore?ocid=TXT_TAGLM_WL_allup_1a_explore_032009
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090312/9b0c6eea/attachment.htm


More information about the Haskell-Cafe mailing list