Last active
May 7, 2017 15:19
-
-
Save osamu/f21170f6ae5f4a1a4fca76b7695de9f1 to your computer and use it in GitHub Desktop.
L99
This file contains 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 palindrome? | |
(lambda (xst) | |
(equal? xst (reverse xst)))) | |
(define flatten | |
(lambda (xst) | |
(cond ((null? xst) '()) | |
((pair? xst) (append (flatten (car xst)) (flatten (cdr xst)))) | |
(else (list xst))))) | |
;; V1 | |
(define (compress xs previous) | |
(cond ((null? xs) '()) | |
((equal? (car xs) previous) (compress (cdr xs) previous)) | |
(else (cons (car xs) (compress (cdr xs) (car xs)))))) | |
(define (compress xs) (compress xs '())) | |
(define (compress2 in out last) | |
(cond ((null? in) (reverse out)) | |
((equal? (car in) last) (compress2 (cdr in) out last)) | |
(else (compress2 (cdr in) (cons (car in) out) (car in))))) | |
(define (compress xs) (compress2 xs '() '())) | |
(compress2 '(a a a b a b b b c c d) '() '()) | |
(define (compress3 in out) | |
(cond ((null? in) (reverse out)) | |
((or (null? out) (not (equal? (car in) (car out)))) (compress3 (cdr in) (cons (car in) out))) | |
(else (compress3 (cdr in) out)))) | |
(define (compress xs) (compress3 xs '())) | |
(compress3 '(a a a b a b b b c c d) '()) | |
(define (pack-iter in sublist out) | |
(cond ((null? in) (reverse out)) | |
((equal? (car in) (car sublist)) (pack-iter (cdr in) (cons (car in) sublist) out)) | |
(else (pack-iter (cdr in) (list (car in)) (cons sublist out))))) | |
(define (pack xs) | |
(if (null? xs) '() | |
(pack-iter (cdr xs) (list (car xs)) '()))) | |
(define (increment-block block) | |
(cons (+ (car block) 1) (cdr block))) | |
(define (encode-iter in block out) | |
(cond ((null? in) (reverse (cons block out))) | |
((equal? (car in) (car (cdr block))) (encode-iter (cdr in) (increment-block block) out)) | |
(else (encode-iter (cdr in) (list 1 (car in)) (cons block out))))) | |
(define (encode xs) | |
(if (null? xs) '() | |
(encode-iter (cdr xs) (list 1 (car xs)) '()))) | |
(define (increment-block block) | |
(cons (+ (car block) 1) (cdr block))) | |
(define (merge-block block out) | |
(if (= (car block) 1) (cons (cadr block) out) | |
(cons block out))) | |
(define (encode-modified-iter in block out) | |
(cond ((null? in) (reverse (merge-block block out))) | |
((equal? (car in) (car (cdr block))) (encode-modified-iter (cdr in) (increment-block block) out)) | |
(else (encode-modified-iter (cdr in) (list 1 (car in)) (merge-block block out))))) | |
(define (encode-modified xs) | |
(if (null? xs) '() | |
(encode-modified-iter (cdr xs) (list 1 (car xs)) '()))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment