I’m reading The Scheme Programming Language book. I’m trying to do exercise 2.8.7:
Use map to define a procedure, transpose, that takes a list of pairs and returns a pair of lists as follows.
(transpose ‘((a . 1) (b . 2) (c . 3))) ;;=> ((a b c) 1 2 3)
[Hint: ((a b c) 1 2 3) is the same as ((a b c) . (1 2 3)).]
I found out that (map list '(a 1) '(b 2) '(c 3)) gives me '((a b c) (1 2 3)). I guess I could solve the exercise by writing a lot of boiler plate for turning '((a . 1) (b . 2) (c . 3)) into (map list '(a 1) '(b 2) '(c 3)) and '((a b c) (1 2 3)) into ((a b c) 1 2 3). However, I’m sure that’s not the point of the exercise.
Can anyone help me out here? Is there an obvious way to do it using map that I’m missing?
Yes. The obvious solution involves calling
maptwice and thenconsing together the two results.