Mutators for compound data Stack Queue

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

1 Lecture 16: Tables and OOP. 2 Tables -- get and put.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
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.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
Fall 2008Programming Development Techniques 1 Topic 19 Mutable Data Objects Section 3.3.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Dotted tail notation (define (proc m1 m2. opt) ) Mandatory Arguments: m1, m2 Mandatory Arguments: m1, m2.
6.001 SICP SICP – October Introduction Trevor Darrell 32-D512 Office Hour: W web page:
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,
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
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.
1 Lecture OO, the notion of inheritance 3 Stacks in OO style (define (make-stack) (let ((top-ptr '())) (define (empty?) (null? top-ptr)) (define.
1 Lecture 16: Lists and vectors Binary search, Sorting.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
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 #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 6 한 태숙.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
1 Lecture 14: Assignment and the Environment Model (EM)
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 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
Abstraction A way of managing complexity for large programs A means of separating details of computation from use of computation Types of Abstraction Data.
Abstract Syntax cs7100 (Prasad) L7AST.
Additional Scheme examples
Intelligent Agents Lecture 3-2 October 14th, 1999 CS250 Lecture 2-1
Edited by Original material by Eric Grimson
6.001 Jeopardy.
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
6.001 SICP Object Oriented Programming
CS 1114: Implementing Search
The role of abstractions
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Lists in Lisp and Scheme
COP4020 Programming Languages
Env. Model Implementation
6.001 SICP Data abstractions
The Metacircular Evaluator
Lecture 15: Tables and OOP
Data abstraction, revisited
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Lecture 16: Tables and OOP
The Metacircular Evaluator (Continued)
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Lecture #9 מבוא מורחב.
Abstract Syntax cs7100 (Prasad) L7AST.
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
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
topics mutable data structures
6.001 SICP Variations on a Scheme
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Lecture 14: The environment model (cont
6.001 SICP Data abstractions
Announcements Quiz 5 HW6 due October 23
Lecture #7 מבוא מורחב.
Defining Functions with DEFUN
Today’s topics Abstractions Procedural Data
6.001 SICP Interpretation Parts of an interpreter
Lecture 13: Assignment and the Environment Model (EM)
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
More Scheme CS 331.
Lists in Lisp and Scheme
Presentation transcript:

Mutators for compound data Stack Queue Section 3.3 Pages 251-273

Mutating Compound Data constructor: (cons x y) creates a new pair p selectors: (car p) returns car part of pair (cdr p) returns cdr part of pair mutators: (set-car! p new-x) changes car pointer in pair (set-cdr! p new-y) changes cdr pointer in pair ; Pair,anytype -> undef -- side-effect only!

Example: Pair/List Mutation (define a (list ‘red ‘blue)) red blue b a a ==> (red blue) 10 X (define b a) b ==> (red blue) (set-car! a 10) a ==> (10 ‘blue) b ==> (10 ‘blue)

Compare this with… red blue c 10 X d c ==> (red blue) d ==> (define c (list ‘red ‘blue)) (define d (list ‘red ‘blue)) red blue c 10 X d c ==> (red blue) d ==> (red blue) (set-car! c 10) c ==> (10 blue) d ==> (red blue)

Equivalent vs. Identical In the first example a and b are identical. A change to one changes the other. The point to a single object. In the second example c and d have at first the same value, but later on one changes and the other does not. They just happen to have at some point in time the same value.

Eq? vs. equal? To check whether two elements are identical, i.e., point to the same object: test with eq? (eq? a b) ==> #t To check whether two elements currently have the same content: test with equal? (equal? (list 1 2) (list 1 2)) ==> #t (eq? (list 1 2) (list 1 2)) ==> #f

F.A.Q. in Philosophy Q: If we change an object, is it the same object? A: Yes, if we retain the same pointer to the object. Q: How can we tell if two elements share the same object? A. One thing you can do is mutate one and see if the other also changes.

Example 2: Pair/List Mutation (define x (list 'a 'b)) (set-car! (cdr x) (list 1 2)) Eval (cdr x) to get a pair object Change car pointer of that pair object a b x 2 1 (a ( 1 2))

Your Turn x ==> (3 4) y ==> (1 2) (set-car! x y) x ==> (set-cdr! y (cdr x)) (set-cdr! x (list 7) 7 X 3 4 x ((1 2) 4) X X ((1 4) 4) 1 2 y ((1 4) 7)

Summary Scheme provides built-in mutators set! to change a binding set-car! and set-cdr! to change a pair Mutation introduces substantial complexity Unexpected side effects Substitution model is no longer sufficient to explain behavior

Implementing cons with set-car! and set-cdr! (define (cons x y) (let ((new (get-new-pair))) (set-car! new x) (set-cdr! new y) new))

Mutation as Assignment (1) (define (cons x y) (define (set-x! val) (set! x val)) (define (set-y! val) (set! y val)) (lambda (m) (cond ((eq? m 'car) x) ((eq? m 'cdr) y) ((eq? m 'set-car!) set-x!) ((eq? m 'set-cdr!) set-y!) (else (error "Undefined operation“ m)))))

Mutation as Assignment (2) (define (car z) (z 'car)) (define (cdr z) (z 'cdr)) (define (set-car! z new-value) ((z 'set-car!) new-value) z) (define (set-cdr! z new-value) ((z 'set-cdr!) new-value)

Stack

Stack Data Abstraction constructor: (make-stack) returns an empty stack selectors: (top stack) returns current top element from a stack operations: (insert stack elem) returns a new stack with the element added to the top of the stack (delete stack) returns a new stack with the top element removed from the stack (empty-stack? stack) returns #t if no elements, #f otherwise

Stack Data Abstraction Insert Insert First in, Last out. Insert Delete Insert

Stack Implementation Strategy implement a stack as a list a b d we will insert and delete items at the front of the stack

Stack Implementation (define (make-stack) nil) (define (empty-stack? stack) (null? stack)) (define (insert stack elem) (cons elem stack)) (define (delete stack) (if (empty-stack? stack) (error "stack underflow – delete") (cdr stack))) (define (top stack) (error "stack underflow – top") (car stack)))

Limitations in our Stack Stack does not have identity (define s (make-stack)) s ==> () (insert s 'a) ==> (a) (set! s (insert s 'b)) s ==> (b)

Alternative Stack Implementation The stack will be a mutable data type. The data type contains a list of elements – as before. Insert and delete mutate a stack object. The first element of the list is special to distinguish Stack objects from other lists – defensive programming X (delete! s) a c d stack s

Alternative Stack Implementation (1) (define (make-stack) (cons 'stack nil)) (define (stack? stack) (and (pair? stack) (eq? 'stack (car stack)))) (define (empty-stack? stack) (if (not (stack? stack)) (error "object not a stack:" stack) (null? (cdr stack)))) (define (top stack) (if (empty-stack? stack) (error "stack underflow – top") (cadr stack)))

Alternative Stack Implementation (2) (define (insert! stack elem) (cond ((not (stack? stack)) (error "object not a stack:" stack)) (else (set-cdr! stack (cons elem (cdr stack))) stack))) (define (delete! stack) (if (empty-stack? stack) (error "stack underflow – delete") (set-cdr! stack (cddr stack))) stack)

Does the user have to know about it? Usually the user does not care about the implementation As long as the contract is being kept. Both implementations keep the contract. Yet: This is a change to the abstraction! The user should know if the object mutates or not in order to use the abstraction correctly. If we (define a b), can a later change because of changes to b?

Queue

Queue Data Abstraction (Non-Mutating) constructor: (make-queue) returns an empty queue accessors: (front-queue q) returns the object at the front of the queue. If queue is empty signals error mutators: (insert-queue q elt) returns a new queue with elt at the rear of the queue (delete-queue q) returns a new queue with the item at the front of the queue removed operations: (empty-queue? q) tests if the queue is empty

Queue illustration Insert Insert First in, First out Insert x1 x2 x3 front

Queue illustration Insert Insert First in, First out Insert Delete x2 front

Queue Contract If q is a queue, created by (make-queue) and subsequent queue procedures, where i is the number of insertions, j is the number of deletions, and xi is the ith item inserted into q , then If j>i then it is an error If j=i then (empty-queue? q) is true, and (front-queue q) and (delete-queue q) are errors. If j<i then (front-queue q) = xj+1

A Queue Implementation A queue is a list of queue elements: c d b The front of the queue is the first element in the list To insert an element at the tail of the queue, we need to “copy” the existing queue onto the front of the new element: b c d new

A Queue Implementation (2) (define (make-queue) nil) (define (empty-queue? q) (null? q)) (define (front-queue q) (if (empty-queue? q) (error "front of empty queue:" q) (car q))) (define (delete-queue q) (error "delete of empty queue:" q) (cdr q))) (define (insert-queue q elt) (cons elt nil) (cons (car q) (insert-queue (cdr q) elt))))

Time complexity of implementation For a queue of length n Time required -- number of cons, car, cdr calls? Space required -- number of new cons cells? front-queue, delete-queue: Time: T(n) = O(1) that is, constant in time Space: S(n) = O(1) that is, constant in space insert-queue: Time: T(n) = O(n) that is, linear in time Space: S(n) = O(n) that is, linear in space

Queue Data Abstraction (Mutating) constructor: (make-queue) returns an empty queue accessors: (front-queue q) returns the object at the front of the queue. If queue is empty signals error mutators: (insert-queue! q elt) inserts the elt at the rear of the queue and returns the modified queue (delete-queue! q) removes the elt at the front of the queue and returns the modified queue operations: (queue? q) tests if the object is a queue (empty-queue? q) tests if the queue is empty

Implementaion We attach a type tag as before. Maintain queue identity Build a structure to hold: a list of items in the queue a pointer to the front of the queue a pointer to the rear of the queue queue c d b a front-ptr rear-ptr

Queue Helper Procedures Hidden inside the abstraction (define (front-ptr q) (cadr q)) (define (rear-ptr q) (cddr q)) (define (set-front-ptr! q item) (set-car! (cdr q) item)) (define (set-rear-ptr! q item) (set-cdr! (cdr q) item)) queue c d b a front-ptr rear-ptr

Queue implementation (define (make-queue) (cons 'queue (cons nil nil))) (define (queue? q) (and (pair? q) (eq? 'queue (car q)))) (define (empty-queue? q) (if (not (queue? q)) (error "object not a queue:" q) (null? (front-ptr q)))) (define (front-queue q) (if (empty-queue? q) (error "front of empty queue:" q) (car (front-ptr q))))

Queue implementation – Insert. (define (insert-queue! q elt) (let ((new-pair (cons elt nil))) (cond ((empty-queue? q) (set-front-ptr! q new-pair) (set-rear-ptr! q new-pair) q) (else (set-cdr! (rear-ptr q) new-pair) q)))) queue c d b a front-ptr rear-ptr e

Queue implementation - delete (define (delete-queue! q) (cond ((empty-queue? q) (error "delete of empty queue:" q)) (else (set-front-ptr! q (cdr (front-ptr q))) q))) queue c d b a front-ptr rear-ptr

Time and Space complexities ?