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)
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)
In newLisp, "apply" can be used for reduce or fold.
(define (compress lst)
(reverse
(apply
(fn (accum x)
(cond ((empty? accum) (list x))
((= x (first accum)) accum)
(true (cons x accum))))
(cons '() lst)
2) ;; How many things to process at a time.
))
(compress '(a a a a b c c a a d e e e e))
===>
(a b c a d e)
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 388 |
Nodes: | 16 (2 / 14) |
Uptime: | 10:07:26 |
Calls: | 8,221 |
Files: | 13,122 |
Messages: | 5,872,631 |