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

Slides:



Advertisements
Similar presentations
Class 21: Imperative Programming University of Virginia cs1120 David Evans.
Advertisements

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Cs1120 Fall 2009 David Evans Lecture 15: Running Practice.
Cs1120 Fall 2009 David Evans Lecture 16: Power Analysis.
David Evans CS200: Computer Science University of Virginia Computer Science Class 27: Modeling Computation.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 16: Quicker Sorting.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 26: In Praise of Idleness.
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
Mutation So far, our data abstractions consists of the following: –Constructors –Selectors/Accessors –Operations –Contract Once a data object is created,
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 4: Introducing Recursive Definitions.
David Evans Class 13: Quicksort, Problems and Procedures CS150: Computer Science University of Virginia Computer Science.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 11: CS Logo, by Lincoln Hamilton and.
ISBN Chapter 15 Functional Programming Languages.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 22: Objectifying Objects.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
Lecture 31: Laziness University of Virginia cs1120 Fall 2009 David Evans.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 9: Strange Loops and Sinister Repeaters.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 20: Objects I invented the term Object-
ISBN Chapter 15 Functional Programming Languages.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 36: Modeling Computing.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 29: Typed Scheme MC Escher, Liberation.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 12: QuickSorting Queen’s University,
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: The Value of Everything.
David Evans CS200: Computer Science University of Virginia Computer Science Class 16: Mutation M. C. Escher, Day and Night.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 5: Recursing on Lists.
6.001 SICP 1/ SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams.
Cs1120 Fall 2009 David Evans Lecture 18: Changing State Sounds of Colossus:
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 15: Intractable Problems (Smiley.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 22: Objects I invented the term Object-
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Lecture 3: Rules of Evaluation CS150: Computer Science
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 14: P = NP?
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: Programming with Data.
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
David Evans CS150: Computer Science University of Virginia Computer Science Class 32: Computability in Theory and Practice.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 8: Cons car cdr sdr wdr.
(Thunking about Thunks)
Lecture 4: Metacircles Eval Apply David Evans
Lecture 17: Environments CS200: Computer Science
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Lecture 13: Quicksorting CS200: Computer Science
Class 30: Models of Computation CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
The Environment Model*
Class 19: Think Globally, Mutate Locally CS150: Computer Science
Lecture 8: Recursion Practice CS200: Computer Science
Lecture 6: Programming with Data CS150: Computer Science
Lecture 11: All Sorts CS200: Computer Science University of Virginia
David Evans Lecture 9: The Great Lambda Tree of Infinite Knowledge and Ultimate Power CS200: Computer Science University.
Lecture 28: Types of Types
Lecture 13: Cost of Sorts CS150: Computer Science
Lecture 22: P = NP? CS200: Computer Science University of Virginia
Lecture 27: In Praise of Idleness CS200: Computer Science
Lecture 26: The Metacircular Evaluator Eval Apply
Lecture 9: The Great Lambda Tree of Knowledge and Power
Lecture 3: Rules of Evaluation CS200: Computer Science
Lecture 11: Sorting Grounds and Bubbles
Lecture 8: Recursing Lists CS150: Computer Science
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

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

27 February 2002CS 200 Spring Menu Mutation Primitives PS 5

27 February 2002CS 200 Spring 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:

27 February 2002CS 200 Spring 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)

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

27 February 2002CS 200 Spring 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!

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

27 February 2002CS 200 Spring 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.

27 February 2002CS 200 Spring 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! !

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

27 February 2002CS 200 Spring > (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: Any reason to be afraid yet?

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

27 February 2002CS 200 Spring 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

27 February 2002CS 200 Spring 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!

27 February 2002CS 200 Spring 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

27 February 2002CS 200 Spring 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

27 February 2002CS 200 Spring 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))

27 February 2002CS 200 Spring 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))

27 February 2002CS 200 Spring 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))

27 February 2002CS 200 Spring 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

27 February 2002CS 200 Spring 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

27 February 2002CS 200 Spring 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