l-99

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

commit 1a46f581487437e481f9fa8780509f27fb8c9950
parent 23898ba605062379c3900687c3d2a690fb79c306
Author: Jenny Doe <tng@soykaf.me>
Date:   Sun, 24 Mar 2019 14:42:39 +0100

added: p09.scm

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

diff --git a/p09.scm b/p09.scm @@ -0,0 +1,30 @@ +; (**) Pack consecutive duplicates of list elements into sublists. +; If a list contains repeated elements they should be placed in separate +; sublists. +; +; Example: +; * (pack '(a a a a b c c a a d e e e e)) +; ((A A A A) (B) (C C) (A A) (D) (E E E 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 (pack lat) + (if (null? lat) + '() + (cons (group lat) + (pack (skip lat)))))