Upload
david-evans
View
353
Download
0
Embed Size (px)
DESCRIPTION
list-accumulate vs. list-cruncherThe Story so Far: Recursive Definitions, Universality, AbstractionProblem Set 3: L-System Fractals
Citation preview
Plan
Accumulator vs. CruncherThe Story so Far: 3 main themes and what’s
coming nextPS3
Reading handout today: excerpt from Gödel, Escher, Bach: An Eternal Golden Braid by Douglas Hofstadter (also on line). You are not required to read it (i.e., there will not be any questions about it on quizzes or exams), but it will embiggen your mind and help you on the path to true enlightenment!
Recap: list-accumulator
(define (list-accumulate f base p) (if (null? p) base (f (car p) (list-accumulate f base (cdr p))))
(define (list-map f p) (list-accumulate (lambda (el rest) (cons (f el) rest)) null p))
(define (list-sum f p) (list-accumulate + 0 p))
(define (list-length f p) (list-accumulate (lambda (el rest) (+ 1 rest)) 0 p))
list-cruncher(define (list-cruncher base carproc combiner p) (if (null? p) base (combiner (carproc (car p)) (list-cruncher baseres carproc combiner (cdr p)))))
Is list-cruncher as powerful as list-accumulator?
Using list-cruncher(define (list-cruncher base carproc combiner p) (if (null? p) base (combiner (carproc (car p)) (list-cruncher baseres carproc combiner (cdr p)))))
(define (list-sum p) (list-cruncher
Can list-cruncher crunch length?(define (list-cruncher baseres carproc combiner p) (if (null? p) baseres (combiner (carproc (car p)) (list-cruncher baseres carproc combiner (cdr p)))))
(define (list-length p) (if (null? p) 0 (+ 1 (list-length (cdr p)))))
Can list-cruncher crunch length?(define (list-cruncher baseres carproc combiner p) (if (null? p) baseres (combiner (carproc (car p)) (list-cruncher baseres carproc combiner (cdr p)))))
(define (list-length p) (if (null? p) 0 (+ 1 (list-length (cdr p)))))
(define (list-length p) (list-cruncher 0 (lambda (x) 1) + p))
Can list-cruncher crunch is-list??(define (list-cruncher baseres carproc combiner p) (if (null? p) baseres (combiner (carproc (car p)) (list-cruncher baseres carproc combiner (cdr p)))))
(define (is-list? p) (if (null? p) true (if (pair? p) (is-list? (cdr p)) false)))
No! If p is non-null, (car p) is always evaluated by list-cruncher so it produces an error if p is not a list.
9
Crunchers vs. Accumulators
(define (list-cruncher baseres carproc combiner p) (if (null? p) baseres (combiner (carproc (car p)) (list-cruncher baseres carproc combiner (cdr p)))))
(define (list-accumulate f base p) (if (null? p) base (f (car p) (list-accumulate f base (cdr p)))))
(define (list-sum p) (list-accumulate + 0 p))
(define (list-sum p) (list-cruncher 0 (lambda (x) x) + p))
10
Crunchers vs. Accumulators
(define (list-cruncher baseres carproc combiner p) (if (null? p) baseres (combiner (carproc (car p)) (list-cruncher baseres carproc combiner (cdr p)))))
(define (list-accumulate f base p) (if (null? p) base (f (car p) (list-accumulate f base (cdr p)))))
Is there any procedure that can be defined using list accumulate‑ that can’t be defined using list-cruncher?
11
(define (list-cruncher baseres carproc combiner p) (if (null? p) baseres (combiner (carproc (car p)) (list-cruncher baseres carproc combiner (cdr p)))))
(define (list-accumulate f base p) (if (null? p) base (f (car p) (list-accumulate f base (cdr p)))))
12
Crunchers vs. Accumulators
(define (list-cruncher baseres carproc combiner p) (if (null? p) baseres (combiner (carproc (car p)) (list-cruncher baseres carproc combiner (cdr p)))))
(define (list-accumulate f base p) (if (null? p) base (f (car p) (list-accumulate f base (cdr p)))))
No! Proof-by-construction:(define (list-accumulate f base p) (list-cruncher base (lambda (x) x) f p))
13
Crunchers vs. Accumulators
(define (list-cruncher baseres carproc combiner p) (if (null? p) baseres (combiner (carproc (car p)) (list-cruncher baseres carproc combiner (cdr p)))))
(define (list-accumulate f base p) (if (null? p) base (f (car p) (list-accumulate f base (cdr p)))))
Gold star bonus: Is there any procedure that can be defined using list cruncher‑ that can’t be defined using list accumulate‑ ? (convincing proof required)
Theme Classes 1-11 Next Week Later
Recursive Definitions
Languages: BNFProceduresData: List
ProceduresData: Trees, etc. Interpreters
Universality Procedures as Inputs and Outputs
Computers as Universal Machines
Proving Universality
AbstractionDefinitions
Procedural AbstractionData Abstraction
Abstract MachinesDigital Abstraction
Objects
Language Abstraction
17
PS3: Lindenmayer System Fractals
Aristid Lindenmayer (1925-1989)
Purple Arrow by Rachel Lathbury and Andrea Yoon
Broccoli Fallout by Paul DiOrio, Rachel Phillips
18
L-Systems
CommandSequence ::= ( CommandList )CommandList ::= Command CommandListCommandList ::= εCommand ::= FCommand ::= RAngleCommand ::= OCommandSequence
Cherry Blossom by Ji Hyun Lee, Wei Wang
19
L-System Rewriting
Start: (F)Rewrite Rule:
F (F O(R30 F) F O(R-60 F) F)
Work like BNF replacement rules, except replace all instances at once!
Why is this a better model for biological systems?
CommandSequence ::= ( CommandList )CommandList ::= Command CommandListCommandList ::=Command ::= FCommand ::= RAngleCommand ::= OCommandSequence
20
Maps are Useful!
PS3 Question 5:
(define (rewrite-lcommands lcommands replacement) (flatten-commands (map ; Procedure to apply to each command lcommands)))
22
Drawing Arbitrary Curves
Rose Bush by Jacintha Henry and Rachel Kay Tie Dye by Bill Ingram
(x, y) = (lambda (t) (make-point …))
23
Curves
A curve c is the set of points that results from applying c to every real number t value between 0.0 and 1.0.
How many points?Infinitely many!
We can draw an approximate representation of the curve by sampling some of the points.
24
Defining Curves
x
y
y = (la
mbda (x) x)
(lambda (t)
(make-point t
t))
1.00.0
1.0
(define diag-line (lambda (t) (make-point t t))
25
Drawing Curves
(define (draw-curve-points curve n) (define (draw-curve-worker curve t step) (if (<= t 1.0) (begin (window-draw-point (curve t)) (draw-curve-worker curve (+ t step) step)))) (draw-curve-worker curve 0.0 (/ 1 n)))
Does this recursive definition have a base case?