racket - Rotate a List of Lists -
i want take arbitrary list of lists , "rotate" it. is, list of lists of size 3x3:
#lang racket (require rackunit) (define (rotate-lol lol) (append (list (map first lol)) (list (map second lol)) (list (map third lol)))) (check-equal? (rotate-lol (list (list 'a 'b 'c) (list 'd 'e 'f) (list 'g 'h 'i))) (list (list 'a 'd 'g) (list 'b 'e 'h) (list 'c 'f 'i)))
i'm thinking list-ref
replace first
/second
/third
can't quite figure out concise way this.
i know there has got elegant way this. i've done inelegant solutions problem in past more domain specific, i'd solve general case, stash in personal library, , done problem. tips or solutions?
it's pretty zip
, don't have make since it's in srfi-1 list library:
(require srfi/1) (zip '(a b c) '(d e f) '(g h i)) ; ==> ((a d g) (b e h) (c f i))
now take parameters list use apply:
(apply zip '((a b c) (d e f) (g h i))) ; ==> ((a d g) (b e h) (c f i))
and, completeness. here how zip
defined:
(define (zip . lsts) (apply map list lsts))
Comments
Post a Comment