מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections 2.1.1 – 2.2.1)

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

CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
מבוא מורחב 1 Lecture #6. מבוא מורחב 2 Primality testing (application) Know how to test whether n is prime in  (log n) time => Can easily find very large.
Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 2: Structured.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Environment Model 3.2, pages
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7. Data directed programming Section 2.4, pages ,2.5.2 pages (but with a different example)
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
מבוא מורחב - שיעור 10 1 Symbols Manipulating lists and trees of symbols: symbolic differentiation Lecture 10.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Dotted tail notation (define (proc m1 m2. opt) ) Mandatory Arguments: m1, m2 Mandatory Arguments: m1, m2.
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.
מבוא מורחב 1 Lecture #13. מבוא מורחב 2 Multiple representations of data.
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
מבוא מורחב - שיעור 12 1 Lecture 12 Data directed programming Message passing dotted-tail notation & apply Section 2.4, pages ,2.5.2 pages.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Data directed programming Message passing Section 2.4, pages ,2.5.2 pages (but with.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7. Outline Symbols Data Directed Programming, Message Passing.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
6.001 SICP 1/ : Structure and Interpretation of Computer Programs Symbols Example of using symbols Differentiation.
Scheme Tutorial. Goals Combine several simple ideas into one compound idea to obtain complex ideas Bring two ideas together to obtain relations Seperate.
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
CS220 Programming Principles 프로그래밍의 이해 2002 년 가을학기 Class 8 한 태숙.
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.
14-October-2002cse Lists © 2002 University of Washington1 Lists CSE 413, Autumn 2002 Programming Languages
Today’s topic: Abstraction Compound Data Data Abstractions: Isolate use of data abstraction from details of implementation Relationship between data abstraction.
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 한 태숙.
Fall 2008Programming Development Techniques 1 Topic 5 Data Abstraction Note: This represents a change in order. We are skipping to chapter 2 without finishing.
1 Data Abstraction. Pairs and Lists. (SICP Sections – 2.2.1)
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Abstraction A way of managing complexity for large programs A means of separating details of computation from use of computation Types of Abstraction Data.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
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
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Lists in Lisp and Scheme
Env. Model Implementation
6.001 SICP Data abstractions
The Metacircular Evaluator
Lecture 18 Infinite Streams and
Lecture #6 section pages pages72-77
Lecture #6 מבוא מורחב.
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
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Lecture #6 section pages pages72-77
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
Lecture #7 מבוא מורחב.
List and list operations (continue).
6.001: Structure and Interpretation of Computer Programs
Today’s topics Abstractions Procedural Data
6.001 SICP Interpretation Parts of an interpreter
Lecture # , , , , מבוא מורחב.
Lecture 13: Assignment and the Environment Model (EM)
Presentation transcript:

מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections – 2.2.1)

מבוא מורחב שיעור 7 2 Procedural abstraction Export only what is needed. Publish: name, number and type of arguments (and conditions they must satisfy) type of procedure’s return value Guarantee: the behavior of the procedure Hide: local variables and procedures, way of implementation, internal details, etc. Interface Implementation

מבוא מורחב שיעור 7 3 Data-object abstraction Export only what is needed. Publish: constructors, selectors Guarantee: the behavior Hide: local variables and procedures, way of implementation, internal details, etc. Interface Implementation

מבוא מורחב שיעור 7 4 An example: Rational numbers We would like to represent rational numbers. A rational number is a quotient a/b of two integers. Constructor: (make-rat a b) Selectors: (numer r) (denom r) Guarantee: (numer (make-rat a b)) = a (denom (make-rat a b)) = b

מבוא מורחב שיעור 7 5 An example: Rational numbers We would like to represent rational numbers. A rational number is a quotient a/b of two integers. Constructor: (make-rat a b) Selectors: (numer r) (denom r) A better Guarantee: (numer (make-rat a b)) (denom (make-rat a b)) = a b A weaker condition, but still sufficient!

מבוא מורחב שיעור 7 6 (add-rat x y) (sub-rat x y) (mul-rat x y) (div-rat x y) (equal-rat? x y) (print-rat x) We can now use the constructors and selectors to implement operations on rational numbers: A form of wishful thinking: we don’t know how make-rat numer and denom are implemented, but we use them.

מבוא מורחב שיעור 7 7 (define (equal-rat? x y) (= (* (numer x) (denom y)) (* (numer y) (denom x)))) Implementing the operations (define (mul-rat x y) (make-rat (* (numer x) (numer y)) (* (denom x) (denom y)))) (define (div-rat x y) (make-rat (* (numer x) (denom y)) (* (denom x) (numer y)))) (define (sub-rat x y) … (define (add-rat x y) ;n1/d1 + n2/d2 = (n1. d2 + n2. d1) / (d1. d2) (make-rat (+ (* (numer x) (denom y)) (* (numer y) (denom x))) (* (denom x) (denom y))))

מבוא מורחב שיעור 7 8 Using the rational package (define (print-rat x) (newline) (display (numer x)) (display ”/”) (display (denom x))) (define one-half (make-rat 1 2)) (print-rat one-half)  1/2 (define one-third (make-rat 1 3)) (print-rat (add-rat one-half one-third))  5/6 (print-rat (add-rat one-third one-third))  6/9

מבוא מורחב שיעור 7 9 Abstraction barriers Programs that use rational numbers add-rat sub-rat mul-rat… make-rat numer denom rational numbers in problem domain rational numbers as numerators and denumerators

מבוא מורחב שיעור 7 10 Gluing things together We still have to implement numer, denom, and make-rat A pair: We need a way to glue things together… (define x (cons 1 2)) (car x)  1 (cdr x)  2

מבוא מורחב שיעור 7 11 Pair: A primitive data type. Constructor: (cons a b) Selectors: (car p) (cdr p) Guarantee: (car (cons a b)) = a (cdr (cons a b)) = b Abstraction barrier: We say nothing about the representation or implementation of pairs.

מבוא מורחב שיעור 7 12 Pairs (define x (cons 1 2)) (define y (cons 3 4)) (define z (cons x y)) (car (car z))  1 ;(caar z) (car (cdr z))  3 ;(cadr z)

מבוא מורחב שיעור 7 13 Implementing make-rat, numer, denom (define (make-rat n d) (cons n d)) (define (numer x) (car x)) (define (denom x) (cdr x))

מבוא מורחב שיעור 7 14 Abstraction barriers Programs that use rational numbers add-rat sub-rat mul-rat... make-rat numer denom cons car cdr rational numbers in problem domain rational numbers as numerators and denumerators rational numbers as pairs

מבוא מורחב שיעור 7 15 Alternative implementation for add-rat (define (add-rat x y) (cons (+ (* (car x) (cdr y)) (* (car y) (cdr x))) (* (cdr x) (cdr y)))) Abstraction Violation If we bypass an abstraction barrier, changes to one level may affect many levels above it. Maintenance becomes more difficult.

מבוא מורחב שיעור 7 16 A solution: change the constructor (define (make-rat a b) (let ((g (gcd a b))) (cons (/ a g) (/ b g)))) In our current implementation we keep 10000/20000 as such and not as 1/2. This: Makes the computation more expensive. Prints out clumsy results. No other changes are required! Rationals - Alternative Implementation

מבוא מורחב שיעור 7 17 Reducing to lowest terms, another way (define (make-rat n d) (cons n d)) (define (numer x) (let ((g (gcd (car x) (cdr x)))) (/ (car x) g))) (define (denom x) (let ((g (gcd (car x) (cdr x)))) (/ (cdr x) g)))

מבוא מורחב שיעור 7 18 How can we implement pairs? (first solution) (define (cons x y) (lambda (f) (f x y))) (define (car z) (z (lambda (x y) x))) (define (cdr z) (z (lambda (x y) y)))

מבוא מורחב שיעור 7 19 (define (cons x y) (lambda (f) (f x y))) ( define (car z) (z (lambda (x y) x))) (define (cdr z) (z (lambda (x y) y))) How can we implement pairs? (first solution, cont’) > (define p (cons 1 2)) Name Value (lambda(f) (f 1 2)) p > (car p) ( (lambda(f) (f 1 2)) (lambda (x y) x)) ( (lambda(x y) x) 1 2 ) > 1

מבוא מורחב שיעור 7 20 How can we implement pairs? (Second solution: message passing) (define (cons x y) (lambda (m) (cond ((= m 0) x) ((= m 1) y) (else (error "Argument not 0 or 1 -- CONS" m)))))) (define (cdr z) (z 1)) (define (car z) (z 0))

מבוא מורחב שיעור 7 21 (define (cons x y) (lambda (m) (cond ((= m 0) x) ((= m 1) y) (else...))) ( define (car z) (z 0)) (define (cdr z) (z 1)) Implementing pairs (second solution, cont’) > (define p (cons 3 4)) (lambda(m) (cond ((= m 0) 3) ((= m 1) 4) (else..))) p > (car p) ((lambda(m) (cond..)) 0) (cond ((= 0 0) 3) ((= 0 1) 4) (else...))) > 3 Name Value

מבוא מורחב שיעור 7 22 Implementation of Pairs - The way it is really done Scheme provides an implementation of pairs, so we do not need to use these “clever” implementations. The natural implementation is by using storage. The two solutions we presented show that the distinction between storage and computation is not always clear. Sometimes we can trade data for computation. The solutions we showed have their own significance: The first is used to show that lambda calculus can simulate other models of computation (theoretical importance). The second – message passing – is the basis for Object Oriented Programming. We will return to it later.

מבוא מורחב שיעור 7 23 Box and Pointer Diagram A pair can be implemented directly using two “pointers”. Originally on IBM 704: (car a) Contents of Address part of Register (cdr a) Contents of Decrement part of Register (define a (cons 1 2)) 2 1 a

מבוא מורחב שיעור 7 24 Box and pointer diagrams (cont.) (cons (cons 1 (cons 2 3)) 4)

מבוא מורחב שיעור 7 25 Compound Data A closure property: 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. Pairs have the closure property: We can pair pairs, pairs of pairs etc. (cons (cons 1 2) 3) 3 2 1

מבוא מורחב שיעור 7 26 Lists (cons 1 (cons 3 (cons 2 ’() ))) Syntactic sugar: (list 1 3 2) The empty list (a.k.a. null or nill)

מבוא מורחב שיעור 7 27 Formal Definition of a List A list is either ’() -- The empty list A pair whose cdr is a list. Lists are closed under the operations cons and cdr: If lst is a non-empty list, then (cdr lst) is a list. If lst is a list and x is arbitrary, then (cons x lst) is a list.

מבוא מורחב שיעור 7 28 Lists (list... ) is syntactic sugar for (cons (cons ( … (cons ’() )))) …

מבוא מורחב שיעור 7 29 Lists (examples) (cdr (list 1 2 3)) (cdr (cons 1 (cons 2 (cons 3 ’() )))) (cons 2 (cons 3 ’() )) (list 2 3) (cons 3 (list 1 2)) (cons 3 (cons 1 (cons 2 ’() ))) (list 3 1 2) The following expressions all result in the same structure: and similarly the following

מבוא מורחב שיעור 7 30 Further List Operations (define one-to-four (list )) one-to-four ==> ( ) ( ) ==> error (car one-to-four) ==> (car (cdr one-to-four)) ==> 1 2 (cadr one-to-four) ==> 2 ( caddr one-to-four) ==> 3

מבוא מורחב שיעור 7 31 More Elaborate Lists (list ) (cons (list 1 2) (list 3 4)) (list (list 1 2) (list 3 4)) Prints as ( ) Prints as ((1 2) 3 4) Prints as ((1 2) (3 4))

מבוא מורחב שיעור 7 32 Yet More Examples  p2 ( (1. 2) (1. 2) ) 1 2 p 3 p1 p2  (define p (cons 1 2))  p (1. 2)  (define p1 (cons 3 p)  p1 (3 1. 2)  (define p2 (list p p))

מבוא מורחב שיעור 7 33 The Predicate Null? null? : anytype -> boolean (null? ) #t if evaluates to empty list #f otherwise (null? 2)  #f (null? (list 1))  #f (null? (cdr (list 1)))  #t (null? ’())  #t (null? null)  #t

מבוא מורחב שיעור 7 34 The Predicate Pair? pair? : anytype -> boolean (pair? ) #t if evaluates to a pair #f otherwise. (pair? (cons 1 2))  #t (pair? (cons 1 (cons 1 2)))  #t (pair? (list 1))  #t (pair? ’())  #f (pair? 3)  #f (pair? pair?)  #f

מבוא מורחב שיעור 7 35 The Predicate Atom? atom? : anytype -> boolean (define (atom? z) (and (not (pair? z)) (not (null? z)))) (define (square x) (* x x)) (atom? square)  #t (atom? 3)  #t (atom? (cons 1 2))  #f

מבוא מורחב שיעור 7 36 More examples  (define digits (list )) ? ( )  (define digits1 (cons 0 digits))  digits1  (define l (list 0 digits))  l ? (0 ( ))

מבוא מורחב שיעור 7 37 The procedure length  (define digits (list ))  (length digits) 9  (define l null)  (length l) 0  (define l (cons 1 l))  (length l) 1 (define (length l) (if (null? l) 0 (+ 1 (length (cdr l)))))