Created
December 23, 2009 00:19
-
-
Save kidd/262204 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
;; '(0 0 1 0 0) => ((2 0) 1 (2 0)) | |
;; CL-USER> (compress '(3 3 4 3 3 2 1 1 1 1 0)) | |
;; ((2 3) 4 (2 3) 2 (4 1) 0) | |
(defun compress (l1) | |
(cond ((null (cdr l1)) '()) | |
(t (accum (car l1) 1 (cdr l1))))) | |
(defun accum (val acc lst) | |
(cond ((null lst) (cons (comp-list val acc) nil)) | |
((eq val (car lst)) (accum val (1+ acc) (cdr lst))) | |
(t (cons (comp-list val acc) (accum (car lst) 1 (cdr lst)))))) | |
(defun comp-list (val acc) | |
(if (> acc 1) (list acc val) val)) | |
(defun decompress (lst) | |
(cond ((null lst) nil) | |
((atom (car lst)) (cons (car lst) (decompress (cdr lst)))) | |
((consp (car lst)) (append (deep-decompress (car lst)) (decompress (cdr lst))) | |
))) | |
(defun deep-decompress (lst) | |
(if (zerop (car lst)) nil | |
(cons (second lst) (deep-decompress (list (1- (car lst)) (second lst)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment