Download presentation
Presentation is loading. Please wait.
Published byBritney Joseph Modified over 6 years ago
1
David Evans http://www.cs.virginia.edu/~evans
Lecture 9: The Great Lambda Tree of Knowledge and Power CS200: Computer Science University of Virginia Computer Science David Evans
2
Programming with Lists PS3
Menu insertl Programming with Lists PS3 6 February 2002 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 6 February 2002 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)))) 6 February 2002 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) … ) 6 February 2002 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)) … )) 6 February 2002 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))))) 6 February 2002 CS 200 Spring 2002
8
Examples > (sum 10) 55 > (insertl * (intsto 5)) 120
> (insertl cons (intsto 10)) cons: expects 2 arguments, given 0 6 February 2002 CS 200 Spring 2002
9
insertl2 (define (insertl2 f lst) (if (= (length lst) 2)
;;; (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))))) 6 February 2002 CS 200 Spring 2002
10
Examples > (insertl2 * (intsto 5)) 120
> (insertl2 cons (intsto 5)) ( ) > (insertl2 (lambda (a b) (cons b a)) (intsto 5)) ((((5 . 4) . 3) . 2) . 1) 6 February 2002 CS 200 Spring 2002
11
insertgen (define (insertlg f lst start) (if (= (length lst) 1)
;;; (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)))) 6 February 2002 CS 200 Spring 2002
12
Examples > (insertlg * (intsto 5) 1) 120 ;;; to copy a list:
(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) ( ) How to define doubleall? (doubleall (intsto 5)) ( ) 6 February 2002 CS 200 Spring 2002
13
doubleall (define (doubleall lst) (insertlg (lambda (a b)
(cons (* 2 a) b)) lst null)) 6 February 2002 CS 200 Spring 2002
14
filter > (filter even? (intsto 9)) (2 4 6 8)
> (filter (lambda (x) (not (= x 3))) (intsto 5)) ( ) 6 February 2002 CS 200 Spring 2002
15
Defining filter (define (filter f lst) (insertlg (lambda (a b)
(if (f a) (cons a b) b)) lst null)) 6 February 2002 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)) 6 February 2002 CS 200 Spring 2002
17
map (define (map f lst) (insertlg (lambda (a b) (cons (f a) b)) lst
null)) 6 February 2002 CS 200 Spring 2002
18
map (define (map f lst) (if (null? lst) null (cons (f (car lst))
(map f (cdr lst))))) 6 February 2002 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)) ( ) > (map (lambda (row) (display-one-row output-file row tile-width tile-height)) tiles) Displays a photomosaic! 6 February 2002 CS 200 Spring 2002
20
PS3: Lindenmayer System Fractals
6 February 2002 CS 200 Spring 2002
21
L-Systems CommandSequence ::= ( CommandList )
CommandList ::= Command CommandList CommandList ::= Command ::= FDistance Command ::= RAngle Command ::= OCommandSequence 6 February 2002 CS 200 Spring 2002
22
L-System Rewriting Start: (F1) Rewrite Rule:
CommandSequence ::= ( CommandList ) CommandList ::= Command CommandList CommandList ::= Command ::= FDistance Command ::= RAngle Command ::= OCommandSequence L-System Rewriting 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? 6 February 2002 CS 200 Spring 2002
23
Level 1 Level 0 (F1) Start: (F1) (F1 O(R30 F1) F1 O(R-60 F1) F1)
24
Level 2 Level 3 6 February 2002 CS 200 Spring 2002
25
6 February 2002 CS 200 Spring 2002
26
Drawing Lambda Tree (define (draw-lambda-tree-of-knowledge)
(draw-region (make-color )) (draw-region (make-color )) (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 ))))) ) 50000)) 6 February 2002 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) 6 February 2002 CS 200 Spring 2002
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.