Loading [MathJax]/extensions/tex2jax.js

2010年7月15日木曜日

Pack consecutive duplicates of list elements into sublists

;; 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)
view raw p09.ss hosted with ❤ by GitHub

0 件のコメント:

コメントを投稿