Skip to content

Instantly share code, notes, and snippets.

@kuanyui
Last active February 9, 2016 16:48
Show Gist options
  • Save kuanyui/182a803d8cac4581821c to your computer and use it in GitHub Desktop.
Save kuanyui/182a803d8cac4581821c to your computer and use it in GitHub Desktop.
(defun run-length (results)
(if (null results)
results
(let ((tmp (run 1 (car results) (cdr results))))
(cons (car tmp) (run-length (cdr tmp))))))
(defun run (times elem remains)
(cond ((equal elem (car remains)) (run (1+ times) elem (cdr remains)))
((= times 1) (cons elem remains))
((> times 1) (cons (list times elem) remains))))
(run-length '(1 1 1 0 0 0 0 0 1 0 1 1 1 0)) ;; => ((3 1) (5 0) 1 0 (3 1) 0)
(define (run-length list)
(define (iterator results)
(if (null? results)
results
(let ((tmp (run 1 (car results) (cdr results))))
(cons (car tmp) (iterator (cdr tmp))))
))
(define (run times elem remains)
(cond ((equal? elem (car remains)) (run (1+ times) elem (cdr remains)))
((= times 1) (cons elem remains))
((> times 1) (cons (list times elem) remains))))
(iterator list))
(run-length '(1 1 1 0 0 0 0 0 1 0 1 1 1 0)) ;; (爆炸)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment