Reputation: 23
i have a list ((x 1) (y 2) (z 3))
and I want to make 2 seprate lists: (x y z)
and
(1 2 3)
I tried using recursive call, using car and cdr, but it didnt work. there is a simple way to do it? Thanks.
Upvotes: 2
Views: 181
Reputation: 11
(apply map list lst) ; returns ((x y z) (1 2 3))
Or use unzip2
from srfi-1.
Upvotes: 1
Reputation: 14222
cdr
returns the tail of the list, which is a list (assuming the input is a list, and not a cons cell). You probably want to use cadr
instead (short-hand for (car (cdr foo))
). You could do:
(map car lst) ; '(x y z)
(map cadr lst) ; '(1 2 3)
(map
will call apply the given function to each item in the list).
Upvotes: 2
Reputation: 38432
with ls as your list: (map car ls) and (map car (map cdr ls))
Upvotes: 0