Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Mutation M. C. Escher, Day and.

Similar presentations


Presentation on theme: "David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Mutation M. C. Escher, Day and."— Presentation transcript:

1 David Evans http://www.cs.virginia.edu/~evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Mutation M. C. Escher, Day and Night

2 27 February 2002CS 200 Spring 20022 Menu Mutation Primitives PS 5

3 27 February 2002CS 200 Spring 20023 Evaluation Rule 2: Names If the expression is a name, it evaluates to the value associated with that name. > (define two 2) > two 2 From Lecture 3:

4 27 February 2002CS 200 Spring 20024 Names and Places A name is not just a value, it is a place for storing a value. define creates a new place, associates a name with that place, and stores a value in that place x: 3 (define x 3)

5 27 February 2002CS 200 Spring 20025 Bang! set! (“set bang”) changes the value associated with a place > (define x 3) > x 3 > (set! x 7) > x 7 x: 3 7

6 27 February 2002CS 200 Spring 20026 set! should make you nervous > (define x 2) > (nextx) 3 > (nextx) 4 > x 4 Before set! all procedures were functions (except for their side-effects). The value of (f) was the same every time you evaluate it. Now it might be different!

7 27 February 2002CS 200 Spring 20027 Defining nextx (define (nextx) (set! x (+ x 1)) x) (define nextx (lambda () (begin (set! x (+ x 1)) x)))) syntactic sugar for

8 27 February 2002CS 200 Spring 20028 Evaluation Rules > (define x 3) > (+ (nextx) x) 7 or 8 > (+ x (nextx)) 9 or 10 DrScheme evaluates application sub- expression left to right, but Scheme evaluation rules allow any order.

9 27 February 2002CS 200 Spring 20029 set-car! and set-cdr! (set-car! p v) Replaces the car of the cons p with v. (set-cdr! p v) Replaces the cdr of the cons p with v. These should scare you even more then set! !

10 27 February 2002CS 200 Spring 200210 > (define pair (cons 1 2)) > pair (1. 2) pair: 1 2

11 27 February 2002CS 200 Spring 200211 > (define pair (cons 1 2)) > pair (1. 2) > (set-car! pair 0) > (car pair) 0 > (cdr pair) 2 > (set-cdr! pair 1) > pair (0. 1) pair: 1 2 0 1 Any reason to be afraid yet?

12 27 February 2002CS 200 Spring 200212 > pair (0. 1) > (set-cdr! pair pair) > (car pair) 0 > (car (cdr pair)) 0 > (car (cdr (cdr pair))) 0 > pair #0=(0. #0#) pair: 1 2 0 1 pair

13 27 February 2002CS 200 Spring 200213 Functional Programming Programming without mutation –Side-effects like printing and drawing on the screen are really mutations (of the display, printer, bell, etc.) If an expression has a value, it is always the same – order of evaluation doesn’t matter Substitution mode of evaluation works fine

14 27 February 2002CS 200 Spring 200214 Imperative Programming Programming with mutation (assignment) Value of an expression might be different depending on when it is evaluated Substitution model of evaluation doesn’t work anymore!

15 27 February 2002CS 200 Spring 200215 Why Substitution Fails? (define (nextx) (set! x (+ x 1)) x) > (define x 0) > ((lambda (x) (+ x x)) (nextx)) 2 Substitution model: (+ (nextx) (nextx)) (+ (begin (set! x (+ x 1)) x) (begin (set! x (+ x 1)) x)) (+ (begin (set! 0 (+ 0 1)) 0) (begin (set! 0 (+ 0 1)) 0)) (+ 0 0) 0

16 27 February 2002CS 200 Spring 200216 Why would a programming language allow mutation? Does it allow us to express computations we couldn’t express without it? No! We can express all computations without mutation. (We’ll see why before the end of the course…) Mutation allows us to express some computations more naturally and efficiently

17 27 February 2002CS 200 Spring 200217 Example Count uses of letters in a message > (count-letters "this sentence does not have six esses") ((#\a. 1) (#\b. 0) … (#\r. 0) (#\s. 7) … (#\z. 0))

18 27 February 2002CS 200 Spring 200218 Constructing the Tally List (define (make-letter-tallys) (for (char->integer #\a) (char->integer #\z) (lambda (accum cno) (append accum (list (cons (integer->char cno) 0)))) null))

19 27 February 2002CS 200 Spring 200219 Imperative Solution (define (count-letters msg) (let ((letter-uses (make-letter-tallys))) (map (lambda (c) (insertlg (lambda (thisletter foundit) (if foundit foundit (if (eq? (car thisletter) c) (begin (set-cdr! thisletter (+ (cdr thisletter) 1)) #t) #f))) letter-uses #f)) (string->list msg)) letter-uses))

20 27 February 2002CS 200 Spring 200220 Functional Solution Need to keep making new lists! If you are not sure mutation is a good thing (and you shouldn’t be), try writing this yourself

21 27 February 2002CS 200 Spring 200221 PS5 Make a primosaic –Photomosaic that avoids reusing tiles Change the tile selection to favor tiles that have not been used much Keep track of how many tiles each tile is used –Mutate the list of tiles each time a tile is selected as the best tile

22 27 February 2002CS 200 Spring 200222 Charge PS5 –You know everything you need to do it after today, so start early! –More open ended than previous problem sets Thursday, 3:30 in Wilson 402 –Talk by Steven Pinker, The Blank Slate, the Noble Savage, and the Ghost in the Machine Friday –A model of computation and evaluation rules that work with mutation


Download ppt "David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Mutation M. C. Escher, Day and."

Similar presentations


Ads by Google