======P9.5.scm======
<code shell>#!/usr/bin/env racket
#lang racket/base</code>
věci kolem deep, stromů, linearizace,
hloubková rekurze na seznamech viz Lekce 9, [[P9.scm]]
======= linearize =======
<hidden linearizace vnořených seznamů, rekurze>
<code scheme>
(define (linearize l)
(cond
((null? l) '())
((list? (car l))
(append (linearize (car l))
(linearize (cdr l))))
(else
(cons (car l) (linearize (cdr l))))))
</code>
dtto:(append (list (car l)) (linearize ...))
</hidden>
<code scheme>
(linearize '((1 2 3) (4 5 6) (7 (8)) 9))
</code>
======= atoms =======
<hidden počet atomů ve vnořených seznamech, rekurze>
<code scheme>
(define (atoms l)
(cond
((null? l) 0)
((list? (car l)) (+ (atoms (car l)) (atoms (cdr l))))
(else (+ 1 (atoms (cdr l))))))
</code>
</hidden>
<code scheme>
(atoms '((1 2 3) (4 5 6) (7 (8)) 9))
</code>
[[#linearize]] [[#atoms]] jsou si podobné, => [[#deep-accum]]
<hidden hloubková akumulační procedura>
<code scheme>
(define (deep-accum combine nula modif l)
(cond
((null? l) nula)
((list? (car l))
(combine (deep-accum combine nula modif (car l))
(deep-accum combine nula modif (cdr l))))
(else
(combine (modif (car l))
(deep-accum combine nula modif (cdr l))))))
</code>
</hidden>
<code scheme>
(define deep9 '((1 2 3) (4 5 6) (7 (8)) 9 (1 (1 1) 2 3)))
(deep-accum + 0 (lambda (x) 1) '((1 2 3) (4 5 6) (7 (8)) 9))
(deep-accum append '() list '((1 2 3) (4 5 6) (7 (8)) 9))
</code>
<hidden počet atomů určité hodnoty>
<code scheme>
(define (deep-count atom l)
(deep-accum + 0 (lambda (x) (if (equal? atom x) 1 0)) l))
</code>
</hidden>
<code scheme>
(deep-count 1 deep9)
(deep-count 2 deep9)
</code>
<hidden přítomnost atomu ve strultuře>
<code scheme>
(define (deep-find atom l)
(deep-accum (lambda (x y) (or x y)) ;combine
#f
(lambda (x) (equal? atom x)) ;modif
l))
</code>
</hidden>
<code scheme>
(deep-find 1 deep9)
(deep-find 22 deep9)
</code>
<hidden hloubkové nahrazování>
<code scheme>
(define (deep-replace p? modif l)
(deep-accum cons
'()
(lambda (x) (if (p? x) (modif x) x))
l))
</code>
</hidden>
<code scheme>
(deep-replace even? - deep9)
</code>
<hidden linearize pomocí apply, append map>
<code scheme>
(define (linearize2 l)
(if (list? l)
(apply append (map linearize2 l))
(list l);hotovo, prazdny nebo atom
))
</code>
</hidden>
<code scheme>
(linearize2 deep9)
</code>
<hidden atoms2 pomocí apply, append map>
<code scheme>
(define (atoms2 l)
(if (list? l)
(apply + (map atoms2 l))
1;hotovo, prazdny nebo atom
))
</code>
</hidden>
<code scheme>
(atoms2 '())
(atoms2 deep9)
</code>
=> deep-accum2, ta ma 2. nedostatky
monoidalní operace -> arbitrary,
<code scheme>
(define (arbitrary mono-op nil)
(lambda args
(foldr mono-op nil args)));vyšší řád, vrací proceduru!
(define cons/n (arbitrary cons '()))
(cons/n 1 2 3 4 5 6)
</code>
todo
<code scheme>
(define (deep-replace2 p? modif l) l)
; vim: syntax=racket
</code>