开发者

Sorting groups using map and accumulate in Scheme

开发者 https://www.devze.com 2023-01-30 03:18 出处:网络
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.

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 开发者_Go百科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!


Try this:

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


(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))
0

精彩评论

暂无评论...
验证码 换一张
取 消