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
;; 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)))))))) |
0 件のコメント:
コメントを投稿