One list for two lists in the schema

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 a recursive call using car and cdr, but it did not work. Is there an easy way to do this? Thank.

+3
source share
3 answers

cdrreturns the tail of a list, which is a list (assuming the input is a list, not a cons cell). You probably want to use it instead cadr(short for (car (cdr foo))). You can do:

(map car lst)  ; '(x y z)
(map cadr lst) ; '(1 2 3)

( mapcalls to apply this function to each element in the list).

+3
source
(apply map list lst) ; returns ((x y z) (1 2 3))

Or use unzip2from srfi-1.

+1
source

with ls as your list: (car ls card) and (car card (cdr ls card))

0
source

All Articles