l-99

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

commit 4ecab87f432c705b30e3ef2cd0bf5bc15994d9d2
parent e181e510efe9804db8b2bd06ff530071ef1927e5
Author: Jenny Doe <tng@soykaf.me>
Date:   Sun, 24 Mar 2019 14:52:06 +0100

added: p11.scm

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

diff --git a/p11.scm b/p11.scm @@ -0,0 +1,35 @@ +; (*) Modified run-length encoding. +; Modify the result of problem P10 in such a way that if an element has no +; duplicates it is simply copied into the result list. Only elements with +; duplicates are transferred as (N E) lists. +; +; Example: +; * (encode-modified '(a a a a b c c a a d e e e e)) +; ((4 A) B (2 C) (2 A) 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-modified lat) + (if (null? lat) + '() + (cons (let ((g (group lat))) + (if (> (length g) 1) + (cons (length g) + (car lat)) + (car lat))) + (encode-modified (skip lat)))))