6.001 SICP Data abstractions

Slides:



Advertisements
Similar presentations
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
Advertisements

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.
מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
מבוא מורחב 1 Lecture #6. מבוא מורחב 2 Primality testing (application) Know how to test whether n is prime in  (log n) time => Can easily find very large.
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
6.001 SICP SICP – October HOPs, Lists, Trees Trevor Darrell 32-D512 Office Hour: W web page:
Data Abstraction… The truth comes out…. What we’re doing today… Abstraction ADT: Dotted Pair ADT: List Box and Pointer List Recursion Deep List Recursion.
6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
מבוא מורחב 1 Lecture #8. מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list )) ==> ( ) (scramble.
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.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
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.
Today’s topic: Abstraction Compound Data Data Abstractions: Isolate use of data abstraction from details of implementation Relationship between data abstraction.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1 Data Abstraction. Pairs and Lists. (SICP Sections – 2.2.1)
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 6. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections – 2.2.1)
Abstraction A way of managing complexity for large programs A means of separating details of computation from use of computation Types of Abstraction Data.
Lecture #5 מבוא מורחב.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Edited by Original material by Eric Grimson
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
6.001 SICP Variations on a Scheme
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
6.001 SICP Object Oriented Programming
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Nondeterministic Evaluation
Higher-Order Procedures
Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things are in.
Lecture #5 מבוא מורחב.
The Metacircular Evaluator
Lecture 18 Infinite Streams and
PPL Sequence Interface.
The Metacircular Evaluator
Lecture #6 section pages pages72-77
Lecture #6 מבוא מורחב.
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
Lecture #8 מבוא מורחב.
6.001 SICP Streams – the lazy way
The Metacircular Evaluator (Continued)
Streams, Delayed Evaluation and a Normal Order Interpreter
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
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Lecture #6 section pages pages72-77
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
Lecture #7 מבוא מורחב.
List and list operations (continue).
6.001: Structure and Interpretation of Computer Programs
Today’s topics Abstractions Procedural Data
Lecture 2 מבוא מורחב.
This Lecture Substitution model
6.001 SICP Interpretation Parts of an interpreter
Lecture # , , , , מבוא מורחב.
Lecture 13: Assignment and the Environment Model (EM)
Introduction to the Lab
Lecture 2 מבוא מורחב.
Good programming practices
Presentation transcript:

6.001 SICP Data abstractions Abstractions and their variations Basic data abstractions Why data abstractions are useful 6.001 SICP

Procedural abstraction Process of procedural abstraction Define formal parameters, capture process in body of procedure Give procedure a name Hide implementation details from user, who just invokes name to apply procedure procedure Output: type Input: type Details of contract for converting input to output 6.001 SICP

Procedural abstraction example: sqrt To find an approximation of square root of x: Make a guess G Improve the guess by averaging G and x/g Keep improving the guess until it is good enough (define try (lambda (guess x) (if (good-enuf? guess x) guess (try (improve guess x) x)))) (define improve (lambda (guess x) (average guess (/ x guess)))) (define average (lambda (a b) (/ (+ a b) 2))) (define good-enuf? (lambda (guess x) (< (abs (- (square guess) x)) 0.001))) (define sqrt (lambda (x) (try 1 x))) 6.001 SICP

The universe of procedures for sqrt average try improve Good-enuf? sqrt 6.001 SICP

The universe of procedures for sqrt average try improve Good-enuf? sqrt 6.001 SICP

sqrt - Block Structure (define (sqrt x) (define (good-enuf? guess) (< (abs (- (square guess) x)) 0.001)) (define (improve guess) (average guess (/ x guess))) (define (sqrt-iter guess) (if (good-enuf? guess) guess (sqrt-iter (improve guess)))) (sqrt-iter 1.0)) good-enuf? improve sqrt x: number : number 6.001 SICP

Clean version of sqrt  easy to add cbrt (define (sqrt x) (fixed-point (average-damp (lambda (y) (/ x y))) 1)) Compare this to Heron’s algorithm from before – same process, but ideas intertwined with code (define (cbrt x) (lambda (y) (/ x (square y)))) 6.001 SICP

Summary of part 1 Procedural abstractions Isolate details of process from its use Designer has choice of which ideas to isolate, in order to support general patterns of computation Good planning in how to collect procedures within higher order structures can make it easier to add new computations 6.001 SICP

Language Elements Primitives prim. data: numbers, strings, booleans primitive procedures Means of Combination procedure application compound data (today) Means of Abstraction naming compound procedures block structure higher order procedures (more today) conventional interfaces – lists (today) data abstraction (next lecture) 6.001 SICP

Compound data Need a way of gluing data elements together into a unit that can be treated as a simple data element Need ways of getting the pieces back out Need a contract between the “glue” and the “unglue” Ideally want the result of this “gluing” to have the property of closure: “the result obtained by creating a compound data structure can itself be treated as a primitive object and thus be input to the creation of another compound object” 6.001 SICP

Pairs (cons cells) (cons <x-exp> <y-exp>) ==> <P> ;type: x, x  Pair Where <x-exp> evaluates to a value <x-val>, and <y-exp> evaluates to a value <y-val> Returns a pair <P> whose car-part is <x-val> and whose cdr-part is <y-val> (car <P>) ==> <x-val> ;type: Pairtype of car part Returns the car-part of the pair <P> (cdr <P>) ==> <y-val> ;type: Pairtype of cdr part Returns the cdr-part of the pair <P> 6.001 SICP

Compound Data Treat a PAIR as a single unit: Can pass a pair as argument Can return a pair as a value (define (make-point x y) (cons x y)) (define (point-x point) (car point)) (define (point-y point) (cdr point)) (define (make-seg pt1 pt2) (cons pt1 pt2)) (define (start-point seg) (car seg)) 5 4 3 2 1 (2, 3) point 1 2 3 4 5 6.001 SICP

Pair Abstraction Constructor ; cons: A,B -> A X B ; cons: A,B -> Pair<A,B> (cons <x> <y>) ==> <P> Accessors ; car: Pair<A,B> -> A (car <P>) ==> <x> ; cdr: Pair<A,B> -> B (cdr <P>) ==> <y> Predicate ; pair? anytype -> boolean (pair? <z>) ==> #t if <z> evaluates to a pair, else #f 6.001 SICP

Pair abstraction Note how there exists a contract between the constructor and the selectors: (car (cons <a> <b> ))  <a> (cdr (cons <a> <b> ))  <b> Note how pairs have the property of closure – we can use the result of a pair as an element of a new pair: (cons (cons 1 2) 3) 6.001 SICP

Conventional interfaces -- lists A list is a data object that can hold an arbitrary number of ordered items. More formally, a list is a sequence of pairs with the following properties: Car-part of a pair in sequence – holds an item Cdr-part of a pair in sequence – holds a pointer to rest of list Empty-list nil – signals no more pairs, or end of list Note that lists are closed under operations of cons and cdr. 6.001 SICP

Conventional Interfaces - Lists (cons <el1> <el2>) (list <el1> <el2> ... <eln>) Predicate ; null? anytype -> boolean (null? <z>) ==> #t if <z> evaluates to empty list <el1> <el2> <el1> <el2> <eln> … 6.001 SICP

… to be really careful For today we are going to create different constructors and selectors for a list (define first car) (define rest cdr) (define adjoin cons) Note how these abstractions inherit closure from the underlying abstractions! 6.001 SICP

Common Pattern #1: cons’ing up a list (define (enumerate-interval from to) (if (> from to) nil (adjoin from (enumerate-interval (+ 1 from) to)))) (e-i 2 4) (if (> 2 4) nil (adjoin 2 (e-i (+ 1 2) 4))) (if #f nil (adjoin 2 (e-i 3 4))) (adjoin 2 (e-i 3 4)) (adjoin 2 (adjoin 3 (e-i 4 4))) (adjoin 2 (adjoin 3 (adjoin 4 (e-i 5 4)))) (adjoin 2 (adjoin 3 (adjoin 4 nil))) (adjoin 2 (adjoin 3 )) (adjoin 2 ) ==> (2 3 4) 4 3 4 2 3 4 6.001 SICP

Common Pattern #2: cdr’ing down a list (define (list-ref lst n) (if (= n 0) (first lst) (list-ref (rest lst) (- n 1)))) (define (length lst) (if (null? lst) (+ 1 (length (rest lst))))) joe (list-ref joe 1) 2 3 4 6.001 SICP

Cdr’ing and Cons’ing Examples (define (copy lst) (if (null? lst) nil ; base case (adjoin (first lst) ; recursion (copy (rest lst))))) (append (list 1 2) (list 3 4)) ==> (1 2 3 4) Strategy: “copy” list1 onto front of list2. (define (append list1 list2) (cond ((null? list1) list2) ; base (else (adjoin (first list1) ; recursion (append (rest list1) list2))))) 6.001 SICP

Common Pattern #3: Transforming a List (define (square-list lst) (if (null? lst) nil (adjoin (square (first lst)) (square-list (rest lst))))) (define (double-list lst) (adjoin (* 2 (first lst)) (double-list (rest lst))))) (define (map proc lst) (adjoin (proc (first lst)) (map proc (rest lst))))) (map square lst)) (map (lambda (x) (* 2 x)) lst)) 6.001 SICP

Common Pattern #4: Filtering a List (define (filter pred lst) (cond ((null? lst) nil) ((pred (first lst)) (adjoin (first lst) (filter pred (rest lst)))) (else (filter pred (rest lst))))) 6.001 SICP

Common Pattern #5: Accumulating Results (define (add-up lst) (if (null? lst) (+ (first lst) (add-up (rest lst))))) (define (mult-all lst) 1 (* (first lst) (mult-all (rest lst))))) (define (accumulate op init lst) init (op (first lst) (accumulate op init (rest lst))))) (accumulate + 0 lst)) 6.001 SICP

Your Turn (define (length lst) (if (null? lst) (+ 1 (length (rest lst))))) (define (accumulate op init lst) init (op (first lst) (accumulate op init (rest lst))))) Rewrite length as an accumulation (define (length lst) (accumulate (lambda (item) 1) lst)) 6.001 SICP

Example of using structures and interfaces (define make-point cons) (define point-x car) (define point-y cdr) Assume that a polygon is an ordered list of points To find the center of figure (define (centroid pts) (let ((total (length pts))) (make-point (/ (accumulate + 0 (map point-x pts)) total) (map point-y pts)) total)))) 6.001 SICP

Example of using structures and interfaces Assume that a polygon is an ordered list of points To shift a figure (define (shift pt offset) (make-point (- (point-x pt) (point-x offset)) (- (point-y pt) (point-y offset)))) (define (shift-figure pts offset) (map (lambda (pt) (shift pt offset)) pts)) 6.001 SICP

Example of using structures and interfaces Assume that a polygon is an ordered list of points To rotate about the center (define (rotate pt) (make-point (- (point-y pt)) (point-x pt))) (define (rotate-figure pts) (let ((center (centroid pts))) (shift-figure (map rotate (shift-figure pts center)) (- (point-x center)) (- (point-y center)))))) 6.001 SICP

Example of using structures and interfaces Assume that a polygon is an ordered list of points To shift and clip (define (shift&clip pts offset) (filter (lambda (pt) (>= (point-x pt) 0)) (map (lambda (pt) (shift pt offset)) pts))) 6.001 SICP

Summary of part 2 Data structures are built on contract of constructors and accessors Data structures have associated procedures for manipulation – typically the recursive structure of the procedure mimics the recursive structure of the data object Building data structures Think about good organization for data Think about how procedures to manipulate structure will mimic organization of data Build and hide details of abstraction from use of abstraction, by relying on contract 6.001 SICP