6.001 SICP 1 6.001 SICP – October 20 6001-Introduction Trevor Darrell 32-D512 Office Hour: W 11 6.001 web page:

Slides:



Advertisements
Similar presentations
1 Lecture 16: Tables and OOP. 2 Tables -- get and put.
Advertisements

Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
6.001: Structure and Interpretation of Computer Programs Symbols Quotation Relevant details of the reader Example of using symbols Alists Differentiation.
1 The metacircular evaluator Names Extend the calculator to store intermediate results as named values (define x (+ 4 5)) store result as x (+ x.
Fall 2008Programming Development Techniques 1 Topic 19 Mutable Data Objects Section 3.3.
6.001 SICP SICP – October Trees Trevor Darrell 32-D512 Office Hour: W web page:
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7 1. Outline More list examples Symbols 2.
6.001 SICP SICP – October HOPs, Lists, Trees Trevor Darrell 32-D512 Office Hour: W web page:
SICP Data abstraction revisited Data structures: association list, vector, hash table Table abstract data type No implementation of an ADT is necessarily.
6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section.
6.001 SICP SICP – October Introduction Trevor Darrell 32-D512 Office Hour: W web page:
SICP Interpretation part 1 Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment.
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.
1 Functional languages (e.g. Scheme, ML) Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition.
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)
Arbitrarily Long Data Structures: Lists and Recursion CMSC Introduction to Computer Programming October 4, 2002.
Plt /12/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.3 Representation Strategies for Data Types.
1 Lecture 16: Lists and vectors Binary search, Sorting.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
Functional Programming in Scheme and Lisp.
CS 603: Programming Language Organization Lecture 10 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
1 Data abstraction, revisited Design tradeoffs: Speed vs robustness modularity ease of maintenance Table abstract data type: 3 versions No implementation.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Spring 2004Programming Development Techniques 1 Topic 11 Sets and their Representation April 2004.
SICP Tagged data Why do we need tags Concept of tags Extended example.
Fall 2008Programming Development Techniques 1 Topic 14 Multiple Representations of Abstract Data – Data Directed Programming and Additivity Section
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
6.001 SICP 1/ SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams.
SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams Instance.
1 Data Abstraction. Pairs and Lists. (SICP Sections – 2.2.1)
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.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
SICP Tagged data Why do we need tags Concept of tags Extended example.
Data Abstraction: Sets
Additional Scheme examples
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
6.001 SICP Object Oriented Programming
6.001: Structure and Interpretation of Computer Programs
The role of abstractions
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Env. Model Implementation
6.001 SICP Data abstractions
The Metacircular Evaluator
Lecture 15: Tables and OOP
FP Foundations, Scheme In Text: Chapter 14.
Data abstraction, revisited
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Lecture 16: Tables and OOP
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
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
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
Lecture 11: Multiple representations of abstract data Message passing Overloading Section 2.4, pages ,2.5.2 pages מבוא.
6.001 SICP Data abstractions
Today’s topics Abstractions Procedural Data
6.001 SICP Interpretation Parts of an interpreter
Introduction to the Lab
Presentation transcript:

6.001 SICP SICP – October Introduction Trevor Darrell 32-D512 Office Hour: W web page: section web page: sets adts tables – assoc tables – hash

6.001 SICP 2 Set data abstractions Represent a set as a list of its elements in which no element appears more than once. (define (element-of-set? x set) (cond ((null? set) false) ((equal? x (car set)) true) (else (element-of-set? x (cdr set))))) (define (element-of-set? x set) (not (null? (filter (lambda (e) (equal? e x)) set)))

6.001 SICP 3 Adjoin-set If the object to be adjoined is already in the set, we just return the set. Otherwise, cons it on! (define (adjoin-set x set) (if (element-of-set? x set) set (cons x set))) We can define union-set in one line using adjoin-set: (define (union-set set1 set2) (accumulate adjoin-set set1 set2))

6.001 SICP 4 Intersection-set Assume we know how to form the intersection of set2 and the cdr of set1… (define (intersection-set set1 set2) (cond ((or (null? set1) (null? set2)) '()) ((element-of-set? (car set1) set2) (cons (car set1) (intersection-set (cdr set1) set2))) (else (intersection-set (cdr set1) set2)))) Alternatively, use filter: (define (intersection-set set1 set2) (filter (lambda (x) (element-in-set? x set2)) set1)))

6.001 SICP 5 Complexity Element-of-set? may have to scan the entire set. Intersection-set does an element-of-set? check for each element of set1 The complexity of intersection/union is: O(n^2) for two sets of size n

6.001 SICP 6 Sets as ordered lists Representation optimized for search: Set elements are listed in increasing order. In checking for the presence of an item, we no longer have to scan the entire set: (define (element-of-set? x set) (cond ((null? set) false) ((= x (car set)) true) ((< x (car set)) false) (else (element-of-set? x (cdr set))))) Thus, the average number of steps required will be about n/2. This is still O(n) growth, but it does save us, on the average, a factor of 2.

6.001 SICP 7 Ordered intersection-set But this really helps with intersection-set. Unordered representation this operation required O(n^2) steps, because we performed a complete scan of set2 for each element of set1. But with the ordered representation, we can use a linear method….how?

6.001 SICP 8 Ordered intersection-set (define (intersection-set set1 set2)

6.001 SICP 9 Ordered intersection-set (define (intersection-set set1 set2) (if (or (null? set1) (null? set2)) '() (let ((x1 (car set1)) (x2 (car set2))) (cond ((= x1 x2)

6.001 SICP 10 Ordered intersection-set (define (intersection-set set1 set2) (if (or (null? set1) (null? set2)) '() (let ((x1 (car set1)) (x2 (car set2))) (cond ((= x1 x2) (cons x1 (intersection-set (cdr set1) (cdr set2)))) ((< x1 x2)

6.001 SICP 11 Ordered intersection-set (define (intersection-set set1 set2) (if (or (null? set1) (null? set2)) '() (let ((x1 (car set1)) (x2 (car set2))) (cond ((= x1 x2) (cons x1 (intersection-set (cdr set1) (cdr set2)))) ((< x1 x2) (intersection-set (cdr set1) set2)) ((< x2 x1) (intersection-set set1 (cdr set2)))))))

6.001 SICP 12 Traditional LISP structure: association list A list where each element is a list of the key and value. 15 x 20 y x: 15 y: 20 Represent the table as the alist: ((x 15) (y 20))

6.001 SICP 13 Alist operation: find-assoc (define (find-assoc key alist) (cond ((null? alist) #f) ((equal? key (caar alist)) (cadar alist)) (else (find-assoc key (cdr alist))))) (define a1 '((x 15) (y 20))) (find-assoc 'y a1) ==> 20

6.001 SICP 14 Alist operation: add-assoc (define (add-assoc key val alist) (cons (list key val) alist)) (define a2 (add-assoc 'y 10 a1)) a2 ==> ((y 10) (x 15) (y 20)) (find-assoc 'y a2) ==> 10

6.001 SICP 15 Alist operation: find-assoc (find-assoc ‘bar ‘((foo 6) (jane 3)) ==> #f (find-assoc ‘bar ‘((foo 6) (bar #f) (ed 4)) ==> #f

6.001 SICP 16 find-assoc-entry (define (find-assoc-entry key alist) (cond ((null? alist) #f) ((equal? key (caar alist)) ??? ) (else (find-assoc-entry key (cdr alist))))) (find-assoc-entry ‘bar ‘((foo 6) (bar #f) (ed 4)) ==> (bar #f)

6.001 SICP 17 find-assoc-entry (define (find-assoc-entry key alist) (cond ((null? alist) #f) ((equal? key (caar alist)) (car alist)) (else (find-assoc-entry key (cdr alist))))) (find-assoc-entry ‘bar ‘((foo 6) (bar #f) (ed 4)) ==> (bar #f)

6.001 SICP 18 Alists are not an abstract data type Missing a constructor: Use quote or list to construct (define a1 '((x 15) (y 20))) There is no abstraction barrier: Definition in scheme language manual: "An alist is a list of pairs, each of which is called an association. The car of an association is called the key." Therefore, the implementation is exposed. User may operate on alists using list operations. (filter (lambda (a) ( ((x 15))

6.001 SICP 19 Why do we care that Alists are not an ADT? Modularity is essential for software engineering Build a program by sticking modules together Can change one module without affecting the rest Alists have poor modularity Programs may use list ops like filter and map on alists These ops will fail if the implementation of alists change Must change whole program if you want a different table To achieve modularity, hide information Hide the fact that the table is implemented as a list Do not allow rest of program to use list operations ADT techniques exist in order to do this

6.001 SICP 20 Table: a set of bindings binding: a pairing of a key and a value Abstract interface to a table: make create a new table put! key value insert a new binding replaces any previous binding of that key get key look up the key, return the corresponding value This definition IS the table abstract data type Code shown later is an implementation of the ADT

6.001 SICP 21 Table1: Table ADT implemented as an Alist (define table1-tag 'table1) (define (make-table1) (cons table1-tag nil)) (define (table1-get tbl key) (find-assoc key (cdr tbl))) (define (table1-put! tbl key val) (set-cdr! tbl (add-assoc key val (cdr tbl))))

6.001 SICP 22 Examples of using tables Fred John Bill People Age Age Job Pay 34

6.001 SICP 23 ADT and tags standard pattern for an ADT with tagged data 1.a variable in the ADT implementation stores the tag 2.attach the tag in the constructor 3.write a predicate that checks the tag 4.operations strip the tags, operate, attach the tag again Use tagged data everywhere (including return values) Using tagged data is only defensive programming if you check the tags and don't use the else branch Traditionally, ADT operations / accessors don't check tags, but paranoia is fine here.

6.001 SICP 24 Table2: Table ADT implemented as hash table (define t2-tag 'table2) (define (make-table2 size hashfunc) (let ((buckets (make-vector size nil))) (list t2-tag size hashfunc buckets))) (define (size-of tbl) (cadr tbl)) (define (hashfunc-of tbl) (caddr tbl)) (define (buckets-of tbl) (cadddr tbl))

6.001 SICP 25 get in table2 (define (table2-get tbl key) (let ((index ((hashfunc-of tbl) key (size-of tbl)))) (find-assoc key (vector-ref (buckets-of tbl) index)))) (define (table2-put! tbl key val) (let ((index ((hashfunc-of tbl) key (size-of tbl))) (buckets (buckets-of tbl))) (vector-set! buckets index (add-assoc key val (vector-ref buckets index)))))

6.001 SICP 26 Table2 example (define tt2 (make-table2 4 hash-a-point)) (table2-put! tt2 (make-point 5 5) 20) (table2-put! tt2 (make-point 5 7) 15) tt2 table2 4 vector 15 point 5,7 20 point 5,5 (table2-get tt2 (make-point 5 5)) (table2-put! tt2 (make-point 5 5) 20) (table2-put! tt2 (make-point 5 7) 15) (table2-get tt2 (make-point 5 5)) (define t2-tag 'table2) (define (make-table2 size hashfunc) (let ((buckets (make-vector size nil))) (list t2-tag size hashfunc buckets))) (define (size-of tbl) (cadr tbl)) (define (hashfunc-of tbl) (caddr tbl)) (define (buckets-of tbl) (cadddr tbl)) (define (table2-get tbl key) (let ((index ((hashfunc-of tbl) key (size-of tbl)))) (find-assoc key (vector-ref (buckets-of tbl) index)))) (define (table2-put! tbl key val) (let ((index ((hashfunc-of tbl) key (size-of tbl))) (buckets (buckets-of tbl))) (vector-set! buckets index (add-assoc key val (vector-ref buckets index)))))

6.001 SICP 27 Table2 example (define tt2 (make-table2 4 hash-a-point)) (table2-put! tt2 (make-point 5 5) 20) (table2-put! tt2 (make-point 5 7) 15) 15 point 5,7 20 point 5,5 (table2-get tt2 (make-point 5 5)) tt2 table2 4 vector

6.001 SICP 28 Table2 example (define tt2 (make-table2 4 hash-a-point)) (table2-put! tt2 (make-point 5 5) 20) (table2-put! tt2 (make-point 5 7) 15) 15 point 5,7 20 point 5,5 (table2-get tt2 (make-point 5 5)) tt2 table2 4 vector

6.001 SICP 29 Table2 example (define tt2 (make-table2 4 hash-a-point)) (table2-put! tt2 (make-point 5 5) 20) (table2-put! tt2 (make-point 5 7) 15) 15 point 5,7 (table2-get tt2 (make-point 5 5)) tt2 table2 4 vector 20 point 5,5

6.001 SICP 30 Table2 example (define tt2 (make-table2 4 hash-a-point)) (table2-put! tt2 (make-point 5 5) 20) (table2-put! tt2 (make-point 5 7) 15) 15 point 5,7 (table2-get tt2 (make-point 5 5)) tt2 table2 4 vector 20 point 5,5

6.001 SICP 31 Table2 example (define tt2 (make-table2 4 hash-a-point)) (table2-put! tt2 (make-point 5 5) 20) (table2-put! tt2 (make-point 5 7) 15) (table2-get tt2 (make-point 5 5)) 20 point 5,5 tt2 table2 4 vector 15 point 5,7

6.001 SICP 32 Table2 example (define tt2 (make-table2 4 hash-a-point)) (table2-put! tt2 (make-point 5 5) 20) (table2-put! tt2 (make-point 5 7) 15) (table2-get tt2 (make-point 5 5)) 15 point 5,7 20 point 5,5 tt2 table2 4 vector

6.001 SICP 33 Table2 example (define tt2 (make-table2 4 hash-a-point)) (table2-put! tt2 (make-point 5 5) 20) (table2-put! tt2 (make-point 5 7) 15) tt2 table2 4 vector 15 point 5,7 20 point 5,5 (table2-get tt2 (make-point 5 5))

6.001 SICP 34 Hash functions What makes a good hash function? not too many indicies indicies are evenly distributed hash function is easy to compute E.g., how to hash products at Wal-mart? price? price/100? price modulo 100? SKU?