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

Slides:



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

David Evans CS200: Computer Science University of Virginia Computer Science Lecture 13: Of On and Off Grounds Sorting.
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.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 18: The Story So Far.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 16: Quicker Sorting.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 31: Types of Types.
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
( (lambda (z) (define x (lambda (x) (lambda (y z) (y x)))) ( ( (x (lambda () z)) (lambda (z) z) 3 ) ) ) 2)
Structuring data So far we’ve seen two types of values that we can bind to names: –numbers –Functions Today we will see two more: –symbols –pairs.
David Evans CS200: Computer Science University of Virginia Computer Science Class 29: Vocational Skills How to Build a.
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
David Evans Class 12: Quickest Sorting CS150: Computer Science University of Virginia Computer Science Rose Bush by Jacintha.
David Evans Class 13: Quicksort, Problems and Procedures CS150: Computer Science University of Virginia Computer Science.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 11: CS Logo, by Lincoln Hamilton and.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
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.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 8.
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 CS150: Computer Science University of Virginia Computer Science Lecture 30: Laziness.
David Evans Class 20: Quick Sorting CS200: Computer Science University of Virginia Computer Science Queen’s University,
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 10: Puzzling Pegboards.
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 12: QuickSorting Queen’s University,
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.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Mutation M. C. Escher, Day and.
Cs1120 Fall 2009 David Evans Lecture 18: Changing State Sounds of Colossus:
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.
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: Vocational Skills (How to Build.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
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
Edited by Original material by Eric Grimson
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Lecture 13: Quicksorting CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
Class 19: Think Globally, Mutate Locally CS150: Computer Science
Class 29: Vocational Skills How to Build a Dynamic Web Site
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 10: Quicker Sorting CS150: Computer Science
Lecture 26: The Metacircular Evaluator Eval Apply
Lecture 13 - Assignment and the environments model Chapter 3
Lecture 9: The Great Lambda Tree of Knowledge and Power
Lecture 3: Rules of Evaluation CS200: Computer Science
Lecture 15: Quicker Sorting CS150: 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 Class 17: Mutation M. C. Escher, Day and Night

25 February 2004CS 200 Spring Menu Mutation Primitives Programming with Mutation PS 5

25 February 2004CS 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:

25 February 2004CS 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)

25 February 2004CS 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

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

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

25 February 2004CS 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.

25 February 2004CS 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! !

25 February 2004CS 200 Spring > (define pair (cons 1 2)) > pair (1. 2) pair: 1 2

25 February 2004CS 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?

25 February 2004CS 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

25 February 2004CS 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 model of evaluation works fine

25 February 2004CS 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!

25 February 2004CS 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

25 February 2004CS 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 –But it also makes everything else more complicated

25 February 2004CS 200 Spring map Functional Solution: A procedure that takes a procedure of one argument and a list, and returns a list of the results produced by applying the procedure to each element in the list. (define (map proc lst) (if (null? lst) null (cons (proc (car lst)) (map proc (cdr lst)))))

25 February 2004CS 200 Spring Imperative Solution A procedure that takes a procedure and list as arguments, and replaces each element in the list with the value of the procedure applied to that element. (define (map! f lst) (if (null? lst) (void) (begin (set-car! lst (f (car lst))) (map! f (cdr lst))))) (define (map proc lst) (if (null? lst) null (cons (proc (car lst)) (map proc (cdr lst)))))

25 February 2004CS 200 Spring Programming with Mutation > (map! square (intsto 4)) > (define i4 (intsto 4)) > (map! square i4) > i4 ( ) > (define i4 (intsto 4)) > (map square i4) ( ) > i4 ( ) Functional Imperative

25 February 2004CS 200 Spring PS5:

25 February 2004CS 200 Spring Databases Database is tables of fields containing values Commands for inserting, selecting and mutating entries in the tables numberlastnamefirstnamecollege 1WashingtonGeorgenone 2AdamsJohnHarvard 3JeffersonThomasWilliam and Mary

25 February 2004CS 200 Spring Representing Tables A table is just a cons pair: –fields (list of fields) –list of entries Each entry is a list of values numberlastnamefirstnamecollege 1WashingtonGeorgenone 2AdamsJohnHarvard 3JeffersonThomasWilliam and Mary (define presidents (make-table (list ‘number ‘lastname ‘firstname ‘college))) (table-insert! presidents (list 1 “Washington” “George” “none”))

25 February 2004CS 200 Spring Selecting Entries numberlastnamefirstnamecollege 1WashingtonGeorgenone 2AdamsJohnHarvard 3JeffersonThomasWilliam and Mary > (table-select presidents ‘college (lambda (college) (not (string=? pitem “Harvard”)))) ((‘number ‘lastname ‘firstname ‘college). ((“Washington” “George” “none”) (“Jefferson” “Thomas” “William and Mary”))

25 February 2004CS 200 Spring PS5 You will implement database commands for selecting and deleting entries from a table, and use your database to implement an auction service The database commands similar to SQL used by commercial database In PS8, you will use SQL to interact with a production quality database (MySQL)

25 February 2004CS 200 Spring Charge PS5 –You know everything you need to do it after today, so start early! Friday –How does mutation effect our evaluation rules? Monday: Quickest Sorting, Golden Ages