l-99

solutions to l-99 (slow)
Log | Files | Refs | Feed

commit e181e510efe9804db8b2bd06ff530071ef1927e5
parent 1a46f581487437e481f9fa8780509f27fb8c9950
Author: Jenny Doe <tng@soykaf.me>
Date:   Sun, 24 Mar 2019 14:46:48 +0100

added: p10.scm

Diffstat:
Ap10.scm | 33+++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+), 0 deletions(-)

diff --git a/p10.scm b/p10.scm @@ -0,0 +1,33 @@ +; (*) Run-length encoding of a list. +; Use the result of problem P09 to implement the so-called run-length +; encoding data compression method. Consecutive duplicates of elements are +; encoded as lists (N E) where N is the number of duplicates of the +; element E. +; +; Example: +; * (encode '(a a a a b c c a a d e e e e)) +; ((4 A) (1 B) (2 C) (2 A) (1 D)(4 E)) + +(define (group lat) + (if (null? lat) + '() + (if (and (not (null? (cdr lat))) + (eq? (car lat) (cadr lat))) + (cons (car lat) + (group (cdr lat))) + (list (car lat))))) + +(define (skip lat) + (if (null? lat) + '() + (if (and (not (null? (cdr lat))) + (eq? (car lat) (cadr lat))) + (skip (cdr lat)) + (cdr lat)))) + +(define (encode lat) + (if (null? lat) + '() + (cons (cons (length (group lat)) + (car lat)) + (encode (skip lat)))))