uni-Bgu
uni-Bgu

Reputation: 23

one list to two lists in scheme

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

Answers (3)

DT'
DT'

Reputation: 11

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

Or use unzip2 from srfi-1.

Upvotes: 1

cam
cam

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

jcomeau_ictx
jcomeau_ictx

Reputation: 38432

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

Upvotes: 0

Related Questions