I got a list of lists in racket and have to transpose them.
(: transpose ((list-of(list-of %a)) -> (list-of (list-of %a))))
(check-expect (transpose (list (list 1 2 3)
(list 4 5 6)))
(list (list 1 4)
(list 2 5)
(list 3 6)))
(define transpose
(lambda (xs)
(cond
((empty? xs)empty)
((pair? xs)(make-pair (make-pair (first(first xs)) (make-pair (first(first(rest xs)))empty)) (transpose (rest(rest xs))))))))
That's my code at the moment. I think the problem is in the recursive call (correct me if I'm wrong please).
The actual outcome is (list (list 1 4))
. The rest seems kinda ignored.
It would really help me, if somebody knows the problem, or has a tip.
The simplest definition of
transpose
is:Why does it work?
Here
List
is spelled with capital letters only to show whichlist
was given by the user and which was produced bymap
.Here is a less "clever" solution. It uses that the first column of a matrix becomes the first row in the transposed matrix.