Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Evans http://www.cs.virginia.edu/evans Lecture 9: The Great Lambda Tree of Infinite Knowledge and Ultimate Power CS200: Computer Science University.

Similar presentations


Presentation on theme: "David Evans http://www.cs.virginia.edu/evans Lecture 9: The Great Lambda Tree of Infinite Knowledge and Ultimate Power CS200: Computer Science University."— Presentation transcript:

1 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

2 Programming with Lists PS3
Menu Programming with Lists PS3 5 February 2003 CS 200 Spring 2003

3 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

4 Sum 5 February 2003 CS 200 Spring 2003

5 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

6 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) ( ) > (intsto 1) (1) Same as previous lecture. (define (gauss-sum n) (insertl (intsto n) + 0)) > (gauss-sum 10) 55 (insertl (list … n) + 0)  (+ 1 (+ 2 (+ 3 (+ … (+ n 0))))) 5 February 2003 CS 200 Spring 2003

7 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) ( ) (define (intsfrom n) (if (= n 0) null (cons n (intsfrom (- n 1))))) 5 February 2003 CS 200 Spring 2003

8 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

9 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)) ( ) Checkup: Try and define append yourself. 5 February 2003 CS 200 Spring 2003

10 Intsto (define (intsto n) (if (= n 0) null
(append (intsto (- n 1)) (list n)))) > (intsto 5) ( ) 5 February 2003 CS 200 Spring 2003

11 Using Intsto (define (gauss-sum n) (insertl (intsto n) + 0))
(define (factorial n) (insertl (intsto n) * 1)) 5 February 2003 CS 200 Spring 2003

12 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

13 map (define (map f lst) (if (null? lst) null (cons (f (car lst))
(map f (cdr lst)))) 5 February 2003 CS 200 Spring 2003

14 map (define (map f lst) (insertl lst (lambda (a b) (cons (f a) b))
null)) 5 February 2003 CS 200 Spring 2003

15 map examples > (map (lambda (x) x) (intsto 5)) (1 2 3 4 5)
> (map (lambda (x) (* x x)) (intsto 5)) ( ) > (map (lambda (row) (display-one-row output-file row tile-width tile-height)) tiles) Displays a photomosaic! 5 February 2003 CS 200 Spring 2003

16 PS3: Lindenmayer System Fractals
5 February 2003 CS 200 Spring 2003

17 L-Systems CommandSequence ::= ( CommandList )
CommandList ::= Command CommandList CommandList ::= Command ::= F Command ::= RAngle Command ::= OCommandSequence 5 February 2003 CS 200 Spring 2003

18 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

19 Level 1 Level 0 (F) Start: (F) (F O(R30 F) F O(R-60 F) F)

20 Level 2 Level 3 5 February 2003 CS 200 Spring 2003

21 The Great Lambda Tree of Ultimate Knowledge and Infinite Power
5 February 2003 CS 200 Spring 2003

22 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


Download ppt "David Evans http://www.cs.virginia.edu/evans Lecture 9: The Great Lambda Tree of Infinite Knowledge and Ultimate Power CS200: Computer Science University."

Similar presentations


Ads by Google