270 likes | 372 Views
Lecture 9: The Great Lambda Tree of Knowledge and Power. David Evans http://www.cs.virginia.edu/~evans. CS200: Computer Science University of Virginia Computer Science. Menu. insertl Programming with Lists PS3. Review. A list is either: a pair where the second part is a list
E N D
Lecture 9: The Great Lambda Tree of Knowledge and Power David Evans http://www.cs.virginia.edu/~evans CS200: Computer Science University of Virginia Computer Science
Menu • insertl • Programming with Lists • PS3 CS 200 Spring 2002
Review • A list is either: a pair where the second part is a list or null (note: book uses nil) • Pair primitives: (cons a b) Construct a pair <a, b> (car pair) First part of a pair (cdr pair) Second part of a pair CS 200 Spring 2002
Sum (define (sum n) (insertl + (intsto n))) ;;; Evaluates to the list (1 2 3 … n) if n >= 1, ;;; null if n = 0. (define (intsto n) (if (= n 0) null (append (intsto (- n 1)) (list n)))) CS 200 Spring 2002
insertl ;;; (insertl f (list a b c d … )) ;;; evaluates to (f a (f b (f c (f d … (f))))) (define (insertl f lst) … ) CS 200 Spring 2002
insertl ;;; (insertl f (list a b c d … )) ;;; evaluates to (f a (f b (f c (f d … (f))))) (define (insertl f lst) (if (null? lst) base case … (insertl f (cdr lst)) … )) CS 200 Spring 2002
insertl ;;; (insertl f (list a b c d … )) ;;; evaluates to (f a (f b (f c (f d … (f))))) (define (insertl f lst) (if (null? lst) (f) (f (car lst) (insertl f (cdr lst))))) CS 200 Spring 2002
Examples > (sum 10) 55 > (insertl * (intsto 5)) 120 > (insertl cons (intsto 10)) cons: expects 2 arguments, given 0 CS 200 Spring 2002
insertl2 ;;; (insertl2 f (list a b c d … y z)) ;;; evaluates to (f a (f b (f c (f d … (f y z))))) (define (insertl2 f lst) (if (= (length lst) 2) (f (car lst) (cadr lst)) (f (car lst) (insertl2 f (cdr lst))))) CS 200 Spring 2002
Examples > (insertl2 * (intsto 5)) 120 > (insertl2 cons (intsto 5)) (1 2 3 4 . 5) > (insertl2 (lambda (a b) (cons b a)) (intsto 5)) ((((5 . 4) . 3) . 2) . 1) CS 200 Spring 2002
insertgen ;;; (insertlg f (list a b c d … y z) start) ;;; evaluates to (f a (f b (f c (f d ;;; … (f y (f z start) (define (insertlg f lst start) (if (= (length lst) 1) (f (car lst) start) (f (car lst) (insertlg f (cdr lst) start)))) CS 200 Spring 2002
(define (insertlg f lst start) (if (= (length lst) 1) (f (car lst) start) (f (car lst) (insertlg f (cdr lst) start)))) Examples > (insertlg * (intsto 5) 1) 120 ;;; to copy a list: > (insertlg cons (intsto 5) null) (1 2 3 4 5) How to define doubleall? (doubleall (intsto 5)) (2 4 6 8 10) CS 200 Spring 2002
doubleall (define (doubleall lst) (insertlg (lambda (a b) (cons (* 2 a) b)) lst null)) CS 200 Spring 2002
filter > (filter even? (intsto 9)) (2 4 6 8) > (filter (lambda (x) (not (= x 3))) (intsto 5)) (1 2 4 5) CS 200 Spring 2002
Defining filter (define (filter f lst) (insertlg (lambda (a b) (if (f a) (cons a b) b)) lst null)) CS 200 Spring 2002
map (map f lst) Evaluates to the list of values produced by applying f to each element of lst. (define (doubleall lst) (map (lambda (s) (* 2 s)) lst)) CS 200 Spring 2002
map (define (map f lst) (insertlg (lambda (a b) (cons (f a) b)) lst null)) CS 200 Spring 2002
map (define (map f lst) (if (null? lst) null (cons (f (car lst)) (map f (cdr lst))))) CS 200 Spring 2002
map examples > (map (lambda (x) x) (intsto 5)) (1 2 3 4 5) > (map (lambda (x) (* x x)) (intsto 5)) (1 4 9 16 25) > (map (lambda (row) (display-one-row output-file row tile-width tile-height)) tiles) Displays a photomosaic! CS 200 Spring 2002
PS3:Lindenmayer System Fractals CS 200 Spring 2002
L-Systems CommandSequence ::= ( CommandList ) CommandList ::= CommandCommandList CommandList ::= Command ::= FDistance Command ::= RAngle Command ::= OCommandSequence CS 200 Spring 2002
L-System Rewriting CommandSequence ::= ( CommandList ) CommandList ::= CommandCommandList CommandList ::= Command ::= FDistance Command ::= RAngle Command ::= OCommandSequence Start: (F1) Rewrite Rule: F1 (F1 O(R30 F1) F1 O(R-60 F1) F1) Work like BNF replacement rules, except replace all instances at once! Why is this a better model for biological systems? CS 200 Spring 2002
Level 1 Level 0 (F1) Start: (F1) F1 (F1 O(R30 F1) F1 O(R-60 F1) F1) (F1 O(R30 F1) F1 O(R-60 F1) F1)
Level 2 Level 3 CS 200 Spring 2002
Drawing Lambda Tree (define (draw-lambda-tree-of-knowledge) (draw-region 0 0 1.0 0.0 0.8 (make-color 108 156 195)) (draw-region 0 0 1.0 0.8 1.0 (make-color 124 232 0)) (draw-curve-points (position-curve (connect-curves-evenly (convert-to-curve-list (make-tree-fractal 4) (lambda () (make-splotch-curve (make-color (+ 128 (random 128)) (+ 128 (random 128)) (random 50)))) (lambda (dist) (make-vertical-line dist (make-color 238 197 45))))) 0.5 0.2) 50000)) CS 200 Spring 2002
Charge • PS3 Due Next Week Weds • Make some interesting fractals • Once you have it working, its easy to produce lots of different pictures • Make “The Great Lambda Tree of Knowledge and Power” (for the CS200 course logo) CS 200 Spring 2002