I struggle understanding this Haskell function. I know what it does superficially, but I'm unsure of how it achieves this functionality.
zip (x:xs) (y:ys) = (x,y) : zip xs ys
zip xs ys = [ ]
What I think:
zip
is the name of the function.zip
takes 2 parameters. (I believe currying is not important here).- The parammeters are
(x:xs)
and(y:ys)
zip
returns a list of a tuple type(x,y)
.
Now I don't quite understand the parameters
(x:xs) (y:ys)
The colon appends something to the start of a list (returning the list), so why do we append something to the lists we want to zip? What are x
and y
in the function definition?
The right side seems pretty obvious: We insert(0) the tuple (x,y) to a list of tuples returned by zip.
(x,y) : zip xs ys
Now zip xs ys = [ ]
why would we always want an empty list if we just pass 2 lists?
Could you explain how the following call to zip
would be evaluation:
zip [5,7,9] [1,3,5,11]