day13

# day13 - CS1: Introduction to Computation Day 13: November...

This preview shows pages 1–16. Sign up to view the full content.

Caltech CS1 -- Fall 2008 CS1: Introduction to Computation Day 13: November 12, 2008 Mutation and Local State

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS1 -- Fall 2008 Godzilla vs. Mothra (1964)
Caltech CS1 -- Fall 2008 Mutation! • Very powerful – Not just for giant radioactive mutant lizards. .. • Computer languages have mutation too! • Mutation can be dangerous. .. • ...but can also make programming much more convenient

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS1 -- Fall 2008 Today • Review: functional style of programming – values never change • Introduce change – AKA mutation – leads to imperative style of programming • Breakdown of substitution model • Replace with environment model
Caltech CS1 -- Fall 2008 Functional Programming • Everything we’ve seen to now has been “functional” Functional in the sense that it is based on a mathematical model of functions • On Day 1, we started from functions – showed how we could turn that into a programming language

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS1 -- Fall 2008 Functional • A mathematical function always returns the same value for the same inputs: f(x) = 2*x + 3 f(3) = 9 (always) f(3) = 9 (still) • This is the definition of a function
Caltech CS1 -- Fall 2008 Functional (fibonacci 6) 8 (factorial 6) 720 (fibonacci 6) 8 (same as before) (factorial 6) 720 (same as before)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS1 -- Fall 2008 Values never change • Once we assign a value – it's always the same – it never changes • x = 6 (define x 6) in Scheme – then x always equals 6 in this context
Caltech CS1 -- Fall 2008 But do have different contexts • factorial(x) = x * factorial(x-1) [for x > 0] i.e. different function calls may have different bindings for x • But within a call (a single function) –the value of any variable never changes

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS1 -- Fall 2008 No change • And a call to a function never changes anything else (f 6) (g 7) (f 8) (f 6) (f 8) ;; return the same thing ;; regardless of call to g (+ (f 6) (g 7) (f 8)) (+ (f 6) (f 8) (g 7)) ;; same value
Caltech CS1 -- Fall 2008 Functional Model • is a be au ti fu l model of computation • Completely capable – can solve any computable problem with it • Easy to reason about • …but it does make programming some things awkward

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS1 -- Fall 2008 Change • Now, will introduce the ability to change values – a variable’s value may change over time (as computation proceeds) i.e. a variable may vary • Once we start using this – the substitution model won’t be correct
Caltech CS1 -- Fall 2008 In other languages. .. • Changing values of variables happens all the time e.g. in C: int y = 10; y = 20; y = y + 30; • In those languages, change is second nature change value of y

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Caltech CS1 -- Fall 2008 Change in Scheme • How can we change the value of variables in Scheme? i.e. to "mutate" their values • Haven't seen this already – intentionally kept you in the dark • Now is the time to reveal the mysteries of mutation
Caltech CS1 -- Fall 2008 set!

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 09/25/2010 for the course CS 1 taught by Professor Chang during the Spring '08 term at UMBC.

### Page1 / 121

day13 - CS1: Introduction to Computation Day 13: November...

This preview shows document pages 1 - 16. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online