overview today’s ideas set-car! and set-cdr!

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

Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
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.
Fall 2008Programming Development Techniques 1 Topic 19 Mutable Data Objects Section 3.3.
6.001 SICP SICP – October environment Trevor Darrell 32-D512 Office Hour: W web page:
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
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.
Mutation So far, our data abstractions consists of the following: –Constructors –Selectors/Accessors –Operations –Contract Once a data object is created,
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
Digital Electronics Data Structures LISP
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is aa good way to learn more about programming languages  Interpreters are.
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.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
1 Lecture 14: Assignment and the Environment Model (EM)
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
Abstraction A way of managing complexity for large programs A means of separating details of computation from use of computation Types of Abstraction Data.
Scheme in Scheme 1.
Edited by Original material by Eric Grimson
6.001 Jeopardy.
6.001 SICP Variations on a Scheme
UNIT III TREES.
The Environment Model*
CSE 341 Lecture 20 Mutation; memoization slides created by Marty Stepp
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.
Class 19: Think Globally, Mutate Locally CS150: Computer Science
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.
The Metacircular Evaluator
J.E. Spragg Mitthögskolan 1997
Lecture 15: Tables and OOP
Data abstraction, revisited
Dynamic Scoping Lazy Evaluation
Lecture #6 section pages pages72-77
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2017.
6.001 SICP Environment model
Lecture 4: Data Abstraction CS201j: Engineering Software
topics three representations entity relationship diagram object model
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
Lecture #9 מבוא מורחב.
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
Data Mutation Primitive and compound data mutators set! for names
6.001 SICP Environment model
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
topics mutable data structures
Lecture #6 section pages pages72-77
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
Dan Grossman / Eric Mullen Autumn 2017
Announcements Quiz 5 HW6 due October 23
Defining Functions with DEFUN
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2016.
Abstraction and Repetition
6.001: Structure and Interpretation of Computer Programs
6.001 SICP Interpretation Parts of an interpreter
6.001 SICP Environment model
Lecture 13: Assignment and the Environment Model (EM)
topics interpreters meta-linguistic abstraction eval and apply
Lisp.
Lists in Lisp and Scheme
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2019.
Presentation transcript:

6001 structure & interpretation of computer programs recitation 11/ october 31, 1997

overview today’s ideas set-car! and set-cdr! aliasing & object equality cyclic data structures 2/24/2019 daniel jackson

mutable data structures how set! differs from set-car! and set-cdr! set! changes a FRAME set-car! and set-cdr! change a CONS CELL mutable structures all data structures are made of cons cells so all we need to modify data structures are set-car! : a way to change what’s in the first compartment set-cdr! : a way to change what’s in the second compartment (in fact, set-car! alone is enough. can you implement a mutable pair with set-car! alone?) contract after (set-car! c x), (car c) evaluates to x after (set-cdr! c y), (cdr c) evaluates to y … assuming that c names a cons cell 2/24/2019 daniel jackson

mutable abstract type example vector abstract type might now have operations like set-x-component! scale-vector! sample code (define (scale-vector! v k) (set-car! v (* k (car v))) (set-cdr! v (* k (cdr v)))) sample use (define v (make-vector 3 4)) (x-coord v) ==> 3 (scale-vector! v 2) (x-coord v) ==> 6 aside: beneficent side effects sometimes we allow operations to mutate the representation so that no changes are visible through the abstraction barrier examples balancing a tree memoizing results of queries 2/24/2019 daniel jackson

a puzzle another version of scale-vector? (define (scale-vector! v k) (set! v (cons (* k (car v)) (* k (cdr v))))) won’t work because creates a new cons cell doesn’t change the existing one draw env diagram for (scale-vector! v k) hangs a new frame in which v points to the same cell as v in the global frame changes this binding, but never changes that cell! 2/24/2019 daniel jackson

aliasing a puzzle (define x (cons 1 2)) (define y x) (car x) ==> 1 (set-car! y 2) (car y) ==> 2 what’s going on? draw box & pointer diagram after (define y x), both vars name the same cons cell so a change through either name is visible through the other terminology two names for the same object are said to be aliases in practice aliasing can be a big problem: may make it hard to understand code but very common in object oriented code 2/24/2019 daniel jackson

object equality can we test whether vars are aliases? if x and y name cons cells (eq? x y) ==> #t when they denote the same one (equal? x y) ==> #t when their values would print out in the same way object equality versus value equality example after (define x (cons 1 2)) (define y (cons 1 2)) (define z y) x and y are value equal, but x and z are object equal (eq? x y) ==> #f (equal? x y) ==> #t (eq? x z) ==> #t (equal? x z) ==> #t so a change to x will appear to be achange to z but not y (set-car! x 3) (car y) ==> 1 (car z) ==> 3 2/24/2019 daniel jackson

cyclic structures puzzle complete (define x …) so that (car x) ==> 1 (car (cdr x)) ==> 1 (car (cdr (cdr x))) ==> 1 (car (cdr (cdr (cdr x)))) ==> 1 … , forever and (eq? x (cdr x)) ==> #t solution code (define x (let ((c (cons 1 nil))) (set-cdr! c c) c)) property (eq? c (cdr c)) ==> #t 2/24/2019 daniel jackson

circular buffer (basic) model always contains at least one element values inserted to right of bar values popped from right of bar pop! returns last value inserted basic operations make-circular-buffer : T –> CB insert! : CB x T –> undefined remove! : CB –> T rotate-left! : CB –> undefined rotate-right! : CB –> undefined size : CB –> int implementation hints use only a singly-linked list same number of cons cells as value in the buffer all ops are 0(1) time except for rotate-right! and size 1 6 2 5 4 3 2/24/2019 daniel jackson