[racket-dev] [PATCH] Add a `zip' procedure to lists

From: J. Ian Johnson (ianj at ccs.neu.edu)
Date: Fri Nov 9 07:34:35 EST 2012

[forgot reply all]
zip is unnecessary because of n-ary map.
(zip l0 l1) = (map list l0 l1)
-Ian
----- Original Message -----
From: "Diogo F. S. Ramos" <diogofsr at gmail.com>
To: dev at racket-lang.org
Sent: Thursday, November 8, 2012 11:46:15 PM GMT -05:00 US/Canada Eastern
Subject: [racket-dev] [PATCH] Add a `zip' procedure to lists

`zip' gathers together each element of each list, in order, returning
a list of those elements.

For example: (zip (list 4 2) (list 3 5)) => '((4 3) (2 5))

Every list has to have the same length.
---
There was talk in the IRC about implementing a `zip' procedure. Here
is my try.

Unfortunately I still don't know what is the preferred way to check
for the validity of the parameters. I tried to copy the style of the
rest of the file.

 collects/racket/list.rkt |    9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/collects/racket/list.rkt b/collects/racket/list.rkt
index fe32f68..18055e5 100644
--- a/collects/racket/list.rkt
+++ b/collects/racket/list.rkt
@@ -32,7 +32,8 @@
          append-map
          filter-not
          shuffle
-         range)
+         range
+         zip)
 
 (define (first x)
   (if (and (pair? x) (list? x))
@@ -407,3 +408,9 @@
     [(end)            (for/list ([i (in-range end)])            i)]
     [(start end)      (for/list ([i (in-range start end)])      i)]
     [(start end step) (for/list ([i (in-range start end step)]) i)]))
+
+(define (zip list1 list2 . lists)
+  (for ((l (list* list1 list2 lists)))
+    (unless (list? l)
+      (raise-argument-error 'zip "list?" l)))
+  (apply map list (list* list1 list2 lists)))
-- 
1.7.9.5

_________________________
  Racket Developers list:
  http://lists.racket-lang.org/dev

Posted on the dev mailing list.