Loading [MathJax]/extensions/tex2jax.js

2010年7月15日木曜日

Eliminate consecutive duplicates of list elements

;; P08 (**) Eliminate consecutive duplicates of list elements.
;; If a list contains repeated elements they should be replaced with a single copy of the element. The order of the elements should not be changed.
;; Example:
;; * (compress '(a a a a b c c a a d e e e e))
;; (A B C A D E)
(define (compress lst)
(if (null? lst)
'()
(let loop ((lst lst) (acc '()))
(let ((head (car lst)) (tail (cdr lst)))
(let ((cha (cons head acc)))
(if (null? tail)
(reverse cha)
(loop tail (if (eq? head (car tail)) acc cha))))))))
view raw p08.ss hosted with ❤ by GitHub

0 件のコメント:

コメントを投稿