This is my second question in Scheme. Say I have 2 lists
'("a" "b" "c")
'("e" "f" "g")
I want to combine them in this way:
'(("a" "e") ("b" "f") ("c" "g"))
Is this possible?
This is my second question in Scheme. Say I have 2 lists
'("a" "b" "c")
'("e" "f" "g")
I want to combine them in this way:
'(("a" "e") ("b" "f") ("c" "g"))
Is this possible?
You use SRFI-1 List library. the function name is zip
. When you apply
zip
to a zip
result you're back to where you started so unzip
is usually the same procedure except it takes a list of lists instead of many list arguments.
(zip '(1 2 3) '(a b c)) ; ==> ((1 a) (2 b) (3 c))
(unzip '((1 2 3) (a b c))) ; ==> ((1 a) (2 b) (3 c))
(unzip '((1 a) (2 b) (3 c))) ; ==> ((1 2 3) (a b c))
If you follow the link you'll find a reference implementation. However you usually don't need it as most implementations do include them. E.g. In Racket R6RS you (import (srfi :1))
or in the racket language you (require srfi/1)
. In Chicken you (declare (uses srfi-1))
so the syntax between the implementations varies greatly.
You tagged lisp
so for completeness a Common Lisp implementation would look like this:
(defun zip (&rest lsts)
(apply #'mapcar #'list lsts))
(defun unzip (lsts)
(apply #'mapcar #'list lsts))