Question

I'm trying to use "map" and "accumulating" functions in scheme for sorting unknown amount of listing into a lists that the first will have all the first places of the olds lists and so on.

(1 2 3.. ) (4 5 6..) (7 8 9..)...

to this list:

(1 4 7) (2 5 8) (3 6 9).

I was writing this:

(accumulate (lambda (x y) (if  (null? y) x (map cons x y))) null '((1 2 3) (4 5 6) (7 8 9) (9 10 11) (12 13 14)))

and it keeps giving me the annoying dot in the end...

((1 4 7 9 . 12) (2 5 8 10 . 13) (3 6 9 11 . 14)).

what seemes to be the problem? thanks!

Was it helpful?

Solution

Try this:

(if (null? y)
    (map list x)
    (map cons x y))

OTHER TIPS

(define (accumulate x . rest)
  (append (list x) rest))

> (map accumulate '(1 2 3) '(4 5 6) '(7 8 9))
=> ((1 4 7) (2 5 8) (3 6 9))
> (map accumulate '(1 2 3 4) '(5 6 7 8) '(9 10 11 12) '(13 14 15 16))
=> ((1 5 9 13) (2 6 10 14) (3 7 11 15) (4 8 12 16))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top