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

David Evans cs.virginia/~evans

  • Upload
    cherie

  • View
    21

  • Download
    2

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: David Evans cs.virginia/~evans

David Evanshttp://www.cs.virginia.edu/~evans

CS200: Computer ScienceUniversity of VirginiaComputer Science

Lecture 9: The Great Lambda Tree of Knowledge and Power

Page 2: David Evans cs.virginia/~evans

6 February 2002 CS 200 Spring 2002 2

Menu

• insertl

• Programming with Lists

• PS3

Page 3: David Evans cs.virginia/~evans

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

Page 4: David Evans cs.virginia/~evans

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

Page 5: David Evans cs.virginia/~evans

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)

)

Page 6: David Evans cs.virginia/~evans

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)) … ))

Page 7: David Evans cs.virginia/~evans

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

Page 8: David Evans cs.virginia/~evans

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

Page 9: David Evans cs.virginia/~evans

6 February 2002 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)))))

Page 10: David Evans cs.virginia/~evans

6 February 2002 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)

Page 11: David Evans cs.virginia/~evans

6 February 2002 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))))

Page 12: David Evans cs.virginia/~evans

6 February 2002 CS 200 Spring 2002 12

Examples

> (insertlg * (intsto 5) 1)120;;; to copy a list:> (insertlg cons (intsto 5) null)(1 2 3 4 5)

(define (insertlg f lst start) (if (= (length lst) 1) (f (car lst) start) (f (car lst) (insertlg f (cdr lst) start))))

How to define doubleall?(doubleall (intsto 5)) (2 4 6 8 10)

Page 13: David Evans cs.virginia/~evans

6 February 2002 CS 200 Spring 2002 13

doubleall

(define (doubleall lst) (insertlg (lambda (a b) (cons (* 2 a) b)) lst null))

Page 14: David Evans cs.virginia/~evans

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))(1 2 4 5)

Page 15: David Evans cs.virginia/~evans

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

Page 16: David Evans cs.virginia/~evans

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

Page 17: David Evans cs.virginia/~evans

6 February 2002 CS 200 Spring 2002 17

map

(define (map f lst) (insertlg (lambda (a b) (cons (f a) b)) lst null))

Page 18: David Evans cs.virginia/~evans

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

Page 19: David Evans cs.virginia/~evans

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))(1 4 9 16 25) > (map (lambda (row) (display-one-row output-file row tile-width tile-height)) tiles)Displays a photomosaic!

Page 20: David Evans cs.virginia/~evans

6 February 2002 CS 200 Spring 2002 20

PS3:Lindenmayer System

Fractals

Page 21: David Evans cs.virginia/~evans

6 February 2002 CS 200 Spring 2002 21

L-Systems

CommandSequence ::= ( CommandList )

CommandList ::= Command CommandList

CommandList ::=

Command ::= FDistance

Command ::= RAngle

Command ::= OCommandSequence

Page 22: David Evans cs.virginia/~evans

6 February 2002 CS 200 Spring 2002 22

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?

CommandSequence ::= ( CommandList )

CommandList ::= Command CommandList

CommandList ::=

Command ::= FDistance

Command ::= RAngle

Command ::= OCommandSequence

Page 23: David Evans cs.virginia/~evans

Level 0

(F1)

Level 1F1 (F1 O(R30 F1) F1 O(R-60 F1) F1)Start: (F1)

(F1 O(R30 F1) F1 O(R-60 F1) F1)

Page 24: David Evans cs.virginia/~evans

6 February 2002 CS 200 Spring 2002 24

Level 2 Level 3

Page 25: David Evans cs.virginia/~evans

6 February 2002 CS 200 Spring 2002 25

Page 26: David Evans cs.virginia/~evans

6 February 2002 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))

Page 27: David Evans cs.virginia/~evans

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)