day13

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

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

View Full Document Right Arrow Icon
Caltech CS1 -- Fall 2008 CS1: Introduction to Computation Day 13: November 12, 2008 Mutation and Local State
Background image of page 1

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

View Full DocumentRight Arrow Icon
Caltech CS1 -- Fall 2008 Godzilla vs. Mothra (1964)
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
Caltech CS1 -- Fall 2008 Functional (fibonacci 6) 8 (factorial 6) 720 (fibonacci 6) 8 (same as before) (factorial 6) 720 (same as before)
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 8
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
Background image of page 9

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

View Full DocumentRight Arrow Icon
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
Background image of page 10
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
Background image of page 11

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

View Full DocumentRight Arrow Icon
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
Background image of page 12
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
Background image of page 13

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

View Full DocumentRight Arrow Icon
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
Background image of page 14
Caltech CS1 -- Fall 2008 set!
Background image of page 15

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

View Full DocumentRight Arrow Icon
Image of page 16
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 Right Arrow Icon
Ask a homework question - tutors are online