As an extra, showcasing the expressive power of standard Scheme, here is an implementation that accepts any number of lists, including none at all.(In other words, it behaves just like Python's built-in zip()
function.)
(define (zip . lists)
(if (null? lists)
'()
(apply map list lists)))
Let's go to the REPL and test that it works as advertised:
> (zip) ; no arguments at all
()
> (zip '(1 2 3)) ; a single argument
(1) (2) (3))
;; Let's conclude by trying four arguments.
> (zip '(1 2 3) '(I II III) '(one two three) '(uno dos tres))
((1 I one uno) (2II two dos) (3 III three tres))
Finally, we make sure that the two-argument tests in the original post continue to pass:
> (zip '(1 2) '(3 4))
((1 3) (2 4))
> (zip '(1 2 3) '())
()
> (zip '() '(4 5 6))
()
> (zip '(8 9) '(3 2 1 4))
((8 3) (9 2))
> (zip '(8 9 1 2) '(3 4))
((8 3) (9 4))
We get all of this for four lines of standard-Scheme code -- no extra libraries, no language extenions. That's not so bad, is it?