Mutation So far, our data abstractions consists of the following: –Constructors –Selectors/Accessors –Operations –Contract Once a data object is created,

Slides:



Advertisements
Similar presentations
Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
Advertisements

CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 The metacircular evaluator Names Extend the calculator to store intermediate results as named values (define x (+ 4 5)) store result as x (+ x.
CS1 Final Exam Review By Rebecca Schulman December 4, 2002.
Fall 2008Programming Development Techniques 1 Topic 19 Mutable Data Objects Section 3.3.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Metacircular Evaluator 4.1, pages definitions file on web 2.
SICP Symbolic data Symbol: a primitive type Generalization Symbolic differentiation.
מבוא מורחב - שיעור 10 1 Symbols Manipulating lists and trees of symbols: symbolic differentiation Lecture 10.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Dotted tail notation (define (proc m1 m2. opt) ) Mandatory Arguments: m1, m2 Mandatory Arguments: m1, m2.
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
SICP Data Mutation Primitive and Compound Data Mutators Stack Example non-mutating mutating Queue Example non-mutating mutating.
1 Functional languages (e.g. Scheme, ML) Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition.
Programming Languages I LISP
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.
SICP Variations on a Scheme (2) Beyond Scheme – designing language variants: Lazy evaluation Complete conversion – normal order evaluator Upward.
1 Lecture 15: More about assignment and the Environment Model (EM)
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is aa good way to learn more about programming languages  Interpreters are.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
1 Lists in Lisp and Scheme. 2 Lists are Lisp’s fundamental data structures. Lists are Lisp’s fundamental data structures. However, it is not the only.
CSE 341 Programming Languages Racket Datatype Style Programming Zach Tatlock Spring 2014.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
1 Data abstraction, revisited Design tradeoffs: Speed vs robustness modularity ease of maintenance Table abstract data type: 3 versions No implementation.
1 You’re Invited! Course VI Freshman Open House! Friday, April 7, :30-5:00 PM FREE Course VI T-Shirts (while supplies last) and Department.
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
Do Now 1)What is a contract? 2) What does it mean when people talk about "the fine print"?
David Evans CS200: Computer Science University of Virginia Computer Science Class 16: Mutation M. C. Escher, Day and Night.
You can access the members of a list with the functions car (or first) and cdr (or rest): (setf list '(a b c)) (car list) ⇒ a (first list) ⇒ a (cdr list)
1 Lecture 20 Lazy Evaluation Continued (4.2.1, 4.2.2) MC-eval examples from exams (Time permitting)
6.001 SICP 1/ SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Mutation M. C. Escher, Day and.
Imperative Programming Chapter 6 1. Local State Real world software like: Banks Course grading system Are state systems. i.e. they change along time:
SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams Instance.
Functional Programming: Lisp MacLennan Chapter 10.
1/32 Data Mutation Primitive and compound data mutators set! for names set-car!, set-cdr! for pairs Stack example non-mutating mutating Queue example non-mutating.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Abstraction A way of managing complexity for large programs A means of separating details of computation from use of computation Types of Abstraction Data.
Edited by Original material by Eric Grimson
6.001 SICP Object Oriented Programming
Programming Languages Dan Grossman 2013
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Lists in Lisp and Scheme
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2018.
Data abstraction, revisited
Dynamic Scoping Lazy Evaluation
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2017.
The Metacircular Evaluator (Continued)
Lecture #9 מבוא מורחב.
Lecture 12: Message passing The Environment Model
Data Mutation Primitive and compound data mutators set! for names
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
overview today’s ideas set-car! and set-cdr!
6.001 SICP Variations on a Scheme
Mutators for compound data Stack Queue
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Lecture 14: The environment model (cont
Defining Functions with DEFUN
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2016.
Functional Programming: Lisp
Lecture 13: Assignment and the Environment Model (EM)
Lists in Lisp and Scheme
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2019.
Presentation transcript:

Mutation So far, our data abstractions consists of the following: –Constructors –Selectors/Accessors –Operations –Contract Once a data object is created, it never changes. Today, we’ll talk about how to alter the internal structure of data objects.

Why Mutation? Saves space- can make small changes to existing object instead of creating new ones. More freedom in creating data types- can freely manipulate pointers to alter structure of cons cells and lists.

Tools of Mutation: basic (set! var x) –Evaluate x. Do not evaluate var. Instead, find its binding and change it to take on the value of x. –Difference between set! and define: define always creates new binding. set! alters existing binding.

Tools of Mutation: Data Structures (set-car! pair x) –Changes car pointer in pair to point to value of x. (set-cdr! pair x) –Changes cdr pointer in pair to point to value of x.

Example (define x (list 1 2 3)) We want to change x to the following: 123 x 123 x

Example x 1 23 x 1 23 We want to change the cdr of the cddr of x to point to x: (set-cdr! (cddr x) x)

Side Effects Mutation can cause unexpected side effects. Example: (define a (list 1 2)) (define b a) (set-car! a 0) a => (0 2) b => (0 2) a b 12 a b 02

Equality 2 tests for equality: –(eq? x y) Tests if x and y point to exactly the same object. If x and y are “eq”, then a change to one should be visible in the other as well –(equal? x y) Tests whether x and y print to the same thing Internal structure of x and y not necessarily the same.

Summary Mutation allows us to change existing bindings and pointers. Saves space and allows more programming freedom. However, must be careful with side effects New notions of equality: –Object equality: eq? –Looks the same: equal?