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)) | |
(require srfi/1) | |
(define (pack lst) | |
(let loop ((lst lst) (acc '())) | |
(if (null? lst) | |
(reverse acc) | |
(loop (strip lst) (cons (picks lst) acc))))) | |
(define-syntax define-pack-aux | |
(syntax-rules () | |
((_ name proc) | |
(define (name lst) | |
(let ((head (car lst))) | |
(proc (lambda (x) | |
(eq? head x)) lst)))))) | |
;; Using a procedure, take-while, defined in SRFI-1 | |
(define-pack-aux picks take-while) | |
;; Using a procedure, drop-while, defined in SRFI-1 | |
(define-pack-aux strip drop-while) |
0 件のコメント:
コメントを投稿