[plt-scheme] "Zipping" two lists?

From: Greg Woodhouse (gregory.woodhouse at sbcglobal.net)
Date: Wed Feb 22 13:28:32 EST 2006

Does Scheme provide a standard way to do this? I wrote a little ad hoc
function, but it seems that this ought to be common enough an operation
to have a standard solution.

;;zip two list (e.g., (zip (1 2) (3 4)) is ((1 3) (2 4)))
;;(list, list) -> list
(define (zip ls1 ls2)
  (if (equal? (length ls1) (length ls2))
      (if (null? ls1) '()
          (cons (list (car ls1) (car ls2))
                (zip (cdr ls1) (cdr ls2))))))

===
Gregory Woodhouse  <gregory.woodhouse at sbcglobal.net>
"All truth passes through three stages: First, it is ridiculed.
Second, it is violently opposed. Third, it is accepted as
being self-evident."
--Arthur Schopenhauer


Posted on the users mailing list.