;; 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 ls . opt)
  (let-optionals* opt ((ep? equal?))
    (pair-fold
     (lambda (pr acc)
       (let/cc hop
         (append acc
                 (if (null? (cdr pr))
                     pr
                     (if (ep? (car pr)(cadr pr))
                         (hop acc)
                         (list (car pr)))))))
     '() ls)))


(compress '(a a a a b c c a a d e e e e))
;; (a b c a d e)
(compress '(a b))
;; (a b)
(compress '(a))
;; (a)
(compress (map (cut cons <> '()) '(a a a a b c c a a d e e e e)) eq?)
;; ((a) (a) (a) (a) (b) (c) (c) (a) (a) (d) (e) (e) (e) (e))
(compress (map (cut cons <> '()) '(a a a a b c c a a d e e e e)))
;; ((a) (b) (c) (a) (d) (e))