Run-length encoding: Difference between revisions

Content added Content deleted
(adding scheme)
Line 2,567: Line 2,567:
(define (run-length-encode s)
(define (run-length-encode s)
(let ((n (string-length s)))
(let ((n (string-length s)))
(let loop ((n (- n 2)) (c (string-ref s (- n 1))) (k 1) (v '()))
(let loop ((i (- n 2)) (c (string-ref s (- n 1))) (k 1) (v '()))
(if (negative? n) (cons (cons k c) v)
(if (negative? i) (cons (cons k c) v)
(let ((x (string-ref s n)))
(let ((x (string-ref s i)))
(if (char=? c x) (loop (- n 1) c (+ k 1) v)
(if (char=? c x) (loop (- i 1) c (+ k 1) v)
(loop (- n 1) x 1 (cons (cons k c) v))))))))
(loop (- i 1) x 1 (cons (cons k c) v))))))))


(run-length-encode "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW")
(run-length-encode "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW")
; ((12 . #\W) (1 . #\B) (12 . #\W) (3 . #\B) (24 . #\W) (1 . #\B) (14 . #\W))
; ((12 . #\W) (1 . #\B) (12 . #\W) (3 . #\B) (24 . #\W) (1 . #\B) (14 . #\W))
(run-length-decode '((12 . #\W) (1 . #\B) (12 . #\W) (3 . #\B) (24 . #\W) (1 . #\B) (14 . #\W)))
(run-length-decode '((12 . #\W) (1 . #\B) (12 . #\W) (3 . #\B) (24 . #\W) (1 . #\B) (14 . #\W)))
; "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"</scheme>
; "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"</lang>


=={{header|sed}}==
=={{header|sed}}==