マイペースに L-99 を続けてきましたが、L27 がなかなか解けなくて滞っています。。
ところで、掲題の通り L09 については、Gauche に group-sequence というものがあったのでメモ。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(use gauche.sequence) | |
(group-sequence '(1 1 1 2 3 4 4 2 2 3 1 1 3)) | |
;; ((1 1 1) (2) (3) (4 4) (2 2) (3) (1 1) (3)) | |
(group-sequence '(a a a a b c c a a d e e e e)) | |
;; ((a a a a) (b) (c c) (a a) (d) (e e e e)) |
自分で書いたのは、こんな感じでした。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; P09 (**) Pack consecutive duplicates of list elements into sublists. | |
;; If a list contains repeated elements they should be placed in separate sublists. | |
;; Example: | |
;; * (pack '(a a a a b c c a a d e e e e)) | |
;; ((A A A A) (B) (C C) (A A) (D) (E E E E)) | |
(define (pack ls . opt) | |
(let-optionals* opt ((eq? eq?)) | |
(pair-fold-right | |
(lambda (pr acc) | |
(apply acons (car pr) | |
(if (or (null? acc) | |
(not (eq? (car pr)(caar acc)))) | |
`(() ,acc) | |
`(,(car acc),(cdr acc))))) | |
'() ls))) | |
(pack '(a a a a b c c a a d e e e e)) | |
;; ((a a a a) (b) (c c) (a a) (d) (e e e e)) | |
(pack '(1)) | |
;; ((1)) | |
(pack '(1 2 3 1 2 3)) | |
;; ((1) (2) (3) (1) (2) (3)) | |
(pack '()) | |
;; () | |
(pack (map (cut cons <> '()) | |
'(a a a a b c c a a d e e e e))) | |
;; (((a)) ((a)) ((a)) ((a)) ((b)) ((c)) ((c)) ((a)) ((a)) ((d)) ((e)) ((e)) ((e)) ((e))) | |
(pack (map (cut cons <> '()) | |
'(a a a a b c c a a d e e e e)) equal?) | |
;; (((a) (a) (a) (a)) ((b)) ((c) (c)) ((a) (a)) ((d)) ((e) (e) (e) (e))) |
0 件のコメント:
コメントを投稿