Run-length encoding: Difference between revisions
Content added Content deleted
(→{{header|Pascal}}: add example) |
(adding scheme) |
||
Line 2,560: | Line 2,560: | ||
To make it faster (it's also faster than the longer implementation above) just replace '''""''' with '''new StringBuilder''' and '''s+i+p''' with '''{s.append(i);s.append(p)}''' |
To make it faster (it's also faster than the longer implementation above) just replace '''""''' with '''new StringBuilder''' and '''s+i+p''' with '''{s.append(i);s.append(p)}''' |
||
=={{header|Scheme}}== |
|||
<lang scheme>(define (run-length-decode v) |
|||
(apply string-append (map (lambda (p) (make-string (car p) (cdr p))) v))) |
|||
(define (run-length-encode s) |
|||
(let ((n (string-length s))) |
|||
(let loop ((n (- n 2)) (c (string-ref s (- n 1))) (k 1) (v '())) |
|||
(if (negative? n) (cons (cons k c) v) |
|||
(let ((x (string-ref s n))) |
|||
(if (char=? c x) (loop (- n 1) c (+ k 1) v) |
|||
(loop (- n 1) x 1 (cons (cons k c) v)))))))) |
|||
(run-length-encode "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW") |
|||
; ((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> |
|||
=={{header|sed}}== |
=={{header|sed}}== |