1 / 27

David Evans cs.virginia/~evans

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

cherie
Download Presentation

David Evans cs.virginia/~evans

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. Menu • insertl • Programming with Lists • PS3 CS 200 Spring 2002

  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 CS 200 Spring 2002

  4. 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

  5. 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

  6. 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

  7. 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

  8. Examples > (sum 10) 55 > (insertl * (intsto 5)) 120 > (insertl cons (intsto 10)) cons: expects 2 arguments, given 0 CS 200 Spring 2002

  9. 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

  10. 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

  11. 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

  12. (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

  13. doubleall (define (doubleall lst) (insertlg (lambda (a b) (cons (* 2 a) b)) lst null)) CS 200 Spring 2002

  14. 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

  15. Defining filter (define (filter f lst) (insertlg (lambda (a b) (if (f a) (cons a b) b)) lst null)) CS 200 Spring 2002

  16. 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

  17. map (define (map f lst) (insertlg (lambda (a b) (cons (f a) b)) lst null)) CS 200 Spring 2002

  18. map (define (map f lst) (if (null? lst) null (cons (f (car lst)) (map f (cdr lst))))) CS 200 Spring 2002

  19. 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

  20. PS3:Lindenmayer System Fractals CS 200 Spring 2002

  21. L-Systems CommandSequence ::= ( CommandList ) CommandList ::= CommandCommandList CommandList ::= Command ::= FDistance Command ::= RAngle Command ::= OCommandSequence CS 200 Spring 2002

  22. 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

  23. 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)

  24. Level 2 Level 3 CS 200 Spring 2002

  25. CS 200 Spring 2002

  26. 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

  27. 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

More Related