topics mutable data structures

Slides:



Advertisements
Similar presentations
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
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.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
1 The metacircular evaluator Names Extend the calculator to store intermediate results as named values (define x (+ 4 5)) store result as x (+ x.
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.
Fall 2008Programming Development Techniques 1 Topic 19 Mutable Data Objects Section 3.3.
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.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
SICP Data Mutation Primitive and Compound Data Mutators Stack Example non-mutating mutating Queue Example non-mutating mutating.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
מבוא מורחב 1 Lecture #8. מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list )) ==> ( ) (scramble.
General pattern for selecting some elements of a list This negatives example illustrates a general pattern: If you want a function which selects some elements.
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.
1 Append: process  (append list1 list2) (cons 1 (append ‘(2) list2)) (cons 1 (cons 2 (append ‘() list2))) (cons 1 (cons 2 list2)) (define (append list1.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 6 한 태숙.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Functional Programming: Lisp MacLennan Chapter 10.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
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.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
Scheme in Scheme 1.
Data Abstraction: Sets
Edited by Original material by Eric Grimson
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
Example of formula (defun roots (a b c) (list
6.001 SICP Variations on a Scheme
6.001 SICP Object Oriented Programming
PPL Lazy Lists.
Racket CSC270 Pepper major portions credited to
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
Nondeterministic Evaluation
6.001 SICP Data abstractions
The Metacircular Evaluator
Lecture 15: Tables and OOP
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Lecture #8 מבוא מורחב.
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
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
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
6.001 SICP Data abstractions
Announcements Quiz 5 HW6 due October 23
Lecture #7 מבוא מורחב.
List and list operations (continue).
Functional Programming: Lisp
6.001 SICP Interpretation Parts of an interpreter
Lecture # , , , , מבוא מורחב.
Lecture 13: Assignment and the Environment Model (EM)
topics interpreters meta-linguistic abstraction eval and apply
To understand recursion, one must first understand recursion.
Lisp.
Presentation transcript:

6001 structure & interpretation of computer programs recitation 12/ november 4, 1997

topics mutable data structures finish circular buffer example from last time sample quiz problems patterns, representations, tree search, streams 2/24/2019 daniel jackson

circular buffer (from last time) model always contains at least one element values inserted to right of bar values removed from right of bar 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 all ops are 0(1) time except for rotate-right! and size 1 6 2 5 4 3 2/24/2019 daniel jackson

implementation notes represent each entry as a cons cell represent buffer as a cons cell whose car (cdr) is the cell to the left (right) of the bar code (define (make-cb v) (let ((cell (cons v nil))) (set-cdr! cell cell) (cons cell cell))) (define (insert! b v) (let ((new-cell (cons v (cdr b)))) (set-cdr! (car b) new-cell) (set-cdr! b new-cell))) (define (remove! b) (let ((old-cell (cdr b))) (set-cdr! (car b) (cdr old-cell)) (set-cdr! b (cdr old-cell)) (car old-cell))) 2/24/2019 daniel jackson

implementation, ctd (define (size b) (define (count cell) (if (eq? (car b) cell) 1 (inc (count (cdr cell))))) (count (cdr b))) ; a hidden operation: (prev c) is the cell that precedes c (define (prev c) (define (follow current) (let ((next (cdr current))) (if (eq? c next) current (follow next)))) 2/24/2019 daniel jackson

implementation, ctd (define (rotate-left! b) (set-car! b (cdr b)) (set-cdr! b (cddr b))) (define (rotate-right! b) (set-cdr! b (car b)) (set-car! b (prev (car b)))) 2/24/2019 daniel jackson

sample problems: pattern matching (1) Write a simplified version of (match pat dat) which takes iin two args, both lists, representing the pattern & the datum. The pattern has ?, ?variable & symbols. The datum has symbols. It returns #f if there is no dictionary or it returns the appropriate dictionary. You can assume you have a procedure (insert var val d) that returns a new dictionary obtainined by adding the pair (var, val) to d, unless var is already there, in which case it returns d if (var, val) is present and #f otherwise. You can also assume that you have primitive procedures var?, anon-var? that take a symbol and return true if the symbol is a variable, anonymous variable examples (match '(? ?a b c ?d) '(hi there b c hello)) ==> ((?a there) (?d hello)) (match '(? ?a b c ?d) '(hi there b bad hello))==> #f 2/24/2019 daniel jackson

sample problems: pattern matching (2) solution (define (match pat dat) (cond ((not (eq? (length pat) (length dat))) #f) ((null? pat) ‘()) (let ((d (match (cdr pat) (cdr dat)))) (if (not d) d (cond ((var? (car pat)) (insert (car pat) (car dat) d)) ((anon-var? (car pat)) d) ((eq? (car pat) (car dat)) d) (else #f)))))) 2/24/2019 daniel jackson

sample problems: pattern matching (3) Write a simplified version of (instantiate temp dict) which takes in two args, a template, which is a list of ?variables & symbols and dict which is a dictionary that better have all the ?variables mentioned in the template. It returns the template with the variables substituted appropriately. example (instantiate '(this ?is a ?test) '((?is hi) (?test hello) (?other ignored))) ==> (this hi a hello) solution (define (instantiate temp dict) (define (lookup v) (cadr (assq v dict))) (map lookup temp)) 2/24/2019 daniel jackson

sample problems: mutable bag Design a Data Structure for.... & implement (or provide a sketch of how it works) the following functions: Represent a mutable bag of elements. A bag is like a set with duplicates. Create empty bag - O(1) add element to bag - O(n) where n is number of distinct elements in bag return total # of elements - O(n) return # of distinct elements - O(n) delete an element - O(n) determine how many occurences of a particular element are in the bag - O(n) After you have something that works with those time bounds, see if you can improve (by changing slightly the data structure) the time bounds for return total # of elements & return # of distinct elements to O(1) Can you think of other useful operations on the bag? 2/24/2019 daniel jackson

sample problem: tree search a tree consists of a key, a value and a list of subtrees write a search procedure that takes a tree and a key and returns a list of all values associated with the key assume you have procedures empty?, key, value, next where (next t) returns the subtrees of t code (define (search t k) (define (dfs trees) (if (null? trees) nil (let ((t1 (car trees))) (if (eq? (key t1) k) (cons (val t1) (dfs (append (next t1) (cdr trees)))) (dfs (append (next t1) (cdr trees))))))) (dfs (list t))) 2/24/2019 daniel jackson

sample problem: streams (1) what does map-stream do? what is its type? implement it using the primitives car-stream, cdr-stream, cons-stream solution map-stream : (t1 -> t2) x stream(t1) –> stream(t2) (define (map-stream p s) (cons-stream (p (car-stream s)) (map-stream p (cdr-stream s)))) 2/24/2019 daniel jackson

sample problem: streams (2) take the tree from before define a procedure that converts a tree to a stream containing key/value pairs in depth first order. use this to implement depth-frst search code (define (streamify t k) (define (dfs trees) (if (null? trees) the-empty-stream (let ((t1 (car trees))) (cons-stream (cons (key t1) (val t1)) (dfs (append (next t1) (cdr trees))))))) (dfs (list t))) (define (search t k) (filter-stream (lambda (e) (eq? (key e) k)) (streamify t))) 2/24/2019 daniel jackson

sample problem: procedures with state write a procedure that takes a procedure p of one argument and returns another procedure, that behaves exactly like p except when presented with the argument ‘how-many, which causes it to return the number of times p has been called. example (define sc (counter square)) (sc 3) ==> 9 (sc 4) ==> 16 (sc ‘how-many) ==> 2 solution (define (counter p) (let ((ct 0)) (lambda (i) (if (eq? i ‘how-many) ct (begin (set! ct (inc ct)) (p i)))))) 2/24/2019 daniel jackson