[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