David Evans http://www.cs.virginia.edu/evans Lecture 9: The Great Lambda Tree of Infinite Knowledge and Ultimate Power CS200: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans
Programming with Lists PS3 Menu Programming with Lists PS3 5 February 2003 CS 200 Spring 2003
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 5 February 2003 CS 200 Spring 2003
Sum 5 February 2003 CS 200 Spring 2003
Gauss Sum (gauss-sum n) = 1 + 2 + … + n (define (gauss-sum n) (if (= n 0) (+ n (gauss-sum (- n 1))))) 5 February 2003 CS 200 Spring 2003
Simpler Gauss Sum? > (intsto 5) (1 2 3 4 5) > (intsto 1) (1) (define (insertl lst f stopval) (if (null? lst) stopval (f (car lst) (insertl (cdr lst) f stopval)))) > (intsto 5) (1 2 3 4 5) > (intsto 1) (1) Same as previous lecture. (define (gauss-sum n) (insertl (intsto n) + 0)) > (gauss-sum 10) 55 (insertl (list 1 2 3 … n) + 0) (+ 1 (+ 2 (+ 3 (+ … (+ n 0))))) 5 February 2003 CS 200 Spring 2003
Reverse intsto (define (intsfrom n) (if (= n 0) null ;;; Evaluates to the list (n n-1 … 3 2 1) ;;; if n >= 1, null if n = 0. > (intsfrom 10) (10 9 8 7 6 5 4 3 2 1) (define (intsfrom n) (if (= n 0) null (cons n (intsfrom (- n 1))))) 5 February 2003 CS 200 Spring 2003
Intsto? (define (intsto n) (if (= n 0) null (list (intsto (- n 1)) n))) > (intsto 5) (((((() 1) 2) 3) 4) 5) 5 February 2003 CS 200 Spring 2003
append > (append (list 3) (list 4)) (3 4) > (append (list 3) null) (3) > (append null (list 3)) > (append (list 1 2 3) (list 4 5 6)) (1 2 3 4 5 6) Checkup: Try and define append yourself. 5 February 2003 CS 200 Spring 2003
Intsto (define (intsto n) (if (= n 0) null (append (intsto (- n 1)) (list n)))) > (intsto 5) (1 2 3 4 5) 5 February 2003 CS 200 Spring 2003
Using Intsto (define (gauss-sum n) (insertl (intsto n) + 0)) (define (factorial n) (insertl (intsto n) * 1)) 5 February 2003 CS 200 Spring 2003
map (map f lst) Evaluates to the list of values produced by applying f to each element of lst. 5 February 2003 CS 200 Spring 2003
map (define (map f lst) (if (null? lst) null (cons (f (car lst)) (map f (cdr lst)))) 5 February 2003 CS 200 Spring 2003
map (define (map f lst) (insertl lst (lambda (a b) (cons (f a) b)) null)) 5 February 2003 CS 200 Spring 2003
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! 5 February 2003 CS 200 Spring 2003
PS3: Lindenmayer System Fractals 5 February 2003 CS 200 Spring 2003
L-Systems CommandSequence ::= ( CommandList ) CommandList ::= Command CommandList CommandList ::= Command ::= F Command ::= RAngle Command ::= OCommandSequence 5 February 2003 CS 200 Spring 2003
L-System Rewriting Start: (F) Rewrite Rule: CommandSequence ::= ( CommandList ) CommandList ::= Command CommandList CommandList ::= Command ::= F Command ::= RAngle Command ::= OCommandSequence 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? 5 February 2003 CS 200 Spring 2003
Level 1 Level 0 (F) Start: (F) (F O(R30 F) F O(R-60 F) F)
Level 2 Level 3 5 February 2003 CS 200 Spring 2003
The Great Lambda Tree of Ultimate Knowledge and Infinite Power 5 February 2003 CS 200 Spring 2003
Charge PS3 Due Next Week Weds Make some interesting fractals Once you have it working, its easy to produce lots of interesting pictures Make a better course logo Don’t leave until you get PS2 and I know your name or take your picture! 5 February 2003 CS 200 Spring 2003