21
Class 21: Changing State cs1120 Fall 2011 David Evans 12 October 2011 Exam 1 Due Now

Class 21: Changing State

Embed Size (px)

DESCRIPTION

Asymptotic Operators PracticeStateset!Mutable pairs

Citation preview

Page 1: Class 21: Changing State

Class 21: Changing State

cs1120 Fall 2011David Evans12 October 2011

Exam 1 Due Now

Page 2: Class 21: Changing State

2

Plan

Asymptotic Operators PracticeIntroducing State

Page 3: Class 21: Changing State

3

Example

Prove 0.001 n2 O(9999n1.9)

Page 4: Class 21: Changing State

4

Remember our Polygon Proof

Theorem: There exists a polygon with four sides.Proof: By construction: here is a polygon

with four sides:

Can we use this strategy to prove 0.001 n2 O(9999n1.9) ?

Page 5: Class 21: Changing State

“Dis-Proofs” are Scarier?

• Theorem: There exists no polygon with two sides.

• Proof:

Page 6: Class 21: Changing State

6

Proof

Prove 0.001 n2 O(9999n1.9)

Page 7: Class 21: Changing State

7

Meaning of

Is a procedure with actually running time 9999n1.9 seconds really worse than a procedure with running time 0.001n2 seconds?

http://www.wolframalpha.com/input/?i=0.001+n^2+%3E+9999n^1.9

Page 8: Class 21: Changing State

Introducing Mutation

Page 9: Class 21: Changing State

Evaluation Rule 2: Names

A name expression evaluates to the value associated with that name.

> (define two 2)> two2

From Lecture 3:

This has been more-or-less okay so far, since the value associated with a name never changes...

Page 10: Class 21: Changing State

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)

Page 11: Class 21: Changing State

Bang!set! (“set bang”) changes the value associated with a place

> (define x 3)> x3> (set! x 7)> x7

x: 37

Page 12: Class 21: Changing State

set! should make you nervous

> (define x 2)> (nextx)3> (nextx)4> x4

Before set! (almost) all procedures were pure functions. The value of (f) was the same every time you evaluate it. Now it might be different!

Page 13: Class 21: Changing State

Defining nextx

(define (nextx) (set! x (+ x 1)) x) (define nextx

(lambda () (begin (set! x (+ x 1)) x))))

syntactic sugar for

Page 14: Class 21: Changing State

Evaluation Rules

> (define x 3)> (+ (nextx) x)7or 8> (+ x (nextx))9or 10

DrScheme evaluates application subexpressions left to right, but Scheme evaluation rules allow any order.

Page 15: Class 21: Changing State

Mutable Cons Cell

mcons – creates a mutable cons cell(mcar m) – first part of a mutable cons cell(mcdr m) – second part of a mutable cons cell

1 2

(mcons 1 2)

Page 16: Class 21: Changing State

set-mcar! and set-mcdr!

(set-mcar! p v)Replaces the car of mutable cons p with v.

(set-mcdr! p v)Replaces the cdr of mutable cons p with v.

These should scare you even more then set!!

Page 17: Class 21: Changing State

> (define pair (mcons 1 2))> pair(1 . 2) pair:

1 2

Page 18: Class 21: Changing State

> (define pair (mcons 1 2))> pair{1 . 2}> (set-mcar! pair 0)> (set-mcdr! pair 1)> pair{0 . 1}> (set-mcdr! pair pair)> pair#0={0 . #0#}

pair:

1 20 1

Page 19: Class 21: Changing State

> (define pair (mcons 1 2))> pair{1 . 2}> (set-mcar! pair 0)> (set-mcdr! pair 1)> pair{0 . 1}> (set-mcdr! pair pair)> pair#0={0 . #0#}

pair:

1 20 1

> (mcar (mcdr pair))0> (mcar (mcdr (mcdr pair)))0> (mcar (mcdr (mcdr (mcdr pair))))0

Page 20: Class 21: Changing State

Impact of Mutation

We will need to revise our evaluation rules for names and application expressions: substitution model of evaluation no longer works: values associated with names change

We need to be much more careful in our programs to think about when things happen: order matters since values change

Page 21: Class 21: Changing State

Charge• PS5: posted now, due Friday, 21 October

• Read through end of Chapter 9• (We will still talk more about Ch 7 and 8)• Friday: Revising evaluation rules to handle

mutation