--- title: Ninety-Nine Lisp Problems – P09 の解答 author: kazu634 date: 2010-11-22 wordtwit_post_info: - 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:5387;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}' categories: - gauche - Lisp ---
L-99: Ninety-Nine Lisp Problemsの問題を解答してみたよ。なんか、エレガントではないなぁ:
;; 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-seed front sublist result l) (cond [(null? l) (append result (list sublist))] [(eq? front (car l)) (pack-seed front (append sublist (list front)) result (cdr l))] [else (pack-seed (car l) (list (car l)) (if (null? result) (if (null? sublist) '() (append result (list sublist))) (append result (list sublist))) (cdr l))])) (define (pack l) (pack-seed #f '() '() l)) (pack '(a a a a b c c a a d e e e e))