Last active
February 9, 2016 16:48
-
-
Save kuanyui/182a803d8cac4581821c 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
(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) |
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
(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