Skip to content

Instantly share code, notes, and snippets.

@kuanyui
Last active August 29, 2015 14:03
Show Gist options
  • Save kuanyui/990903bd5f0ea941f7a2 to your computer and use it in GitHub Desktop.
Save kuanyui/990903bd5f0ea941f7a2 to your computer and use it in GitHub Desktop.
Ugly and dumb solution for fucking run-length homework in PG's ACL.
(defun run-length (input)
(let (l fin)
(mapcar (lambda (x)
(if (null x)
(setq x "nil"))
(cond ((null l)
(setq fin (list (list 1 x)))
(setq l x))
((equal x l)
(setq fin (cons (list (1+ (car (car fin))) x) (cdr fin))))
(t
(setq fin (cons (list 1 x) fin))
(setq l x))))
input)
(reverse
(mapcar (lambda (x)
(if (eq (car x) 1)
(cadr x)
x))
fin))))
;; (run-length '(a a a b c c c c c d e e))
;; => ((3 a) b (5 c) d (2 e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment