מבוא מורחב 1 Lecture #13. מבוא מורחב 2 Multiple representations of data.

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 Lecture 16: Tables and OOP. 2 Tables -- get and put.
Fall 2008Programming Development Techniques 1 Topic 12 Multiple Representations of Abstract Data – Complex Numbers Section
Structure and Interpretation of Computer Programs* Section 2.4 – 2.5 Presenter: Xiang Zhang 05/04/ * Harold A., Gerald J. S., Julie S.. Structure.
데이타를 여러방식으로 구현할 때 Multiple Representations 데이터 구현방식마다 꼬리표를 붙이고. 데이터 속내용 감추기 원리를 유지하면서. 예를 들어 complex number data 구현방안들을 생각해 보자 – complex number 만들기 make-from-real-imag:
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
11.2 Geometric Representations of Complex Numbers.
מבוא מורחב 1 Lecture #6. מבוא מורחב 2 Primality testing (application) Know how to test whether n is prime in  (log n) time => Can easily find very large.
Fall 2008Programming Development Techniques 1 Topic 15 Generic Arithmetic Operations Section &
מבוא מורחב למדעי המחשב בשפת 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.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Dotted tail notation (define (proc m1 m2. opt) ) Mandatory Arguments: m1, m2 Mandatory Arguments: m1, m2.
מבוא מורחב - שיעור 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.
מבוא מורחב 1 Lecture #8. מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list )) ==> ( ) (scramble.
Scheme Tutorial. Goals Combine several simple ideas into one compound idea to obtain complex ideas Bring two ideas together to obtain relations Seperate.
Trees! =) Write up-add tree which behaves in this manner: 1 31 / \ / \ 3 5  21 9 / | \ \ / | \ \
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.
Fall 2008Programming Development Techniques 1 Topic 13 Multiple Representations of Abstract Data – Tagged Data Section
1/38. 2/38 Tagged Data Tag: a symbol in a data structure that identifies its type Why we need tags Extended example: evaluating arithmetic expressions.
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
SICP Tagged data Why do we need tags Concept of tags Extended example.
Fall 2008Programming Development Techniques 1 Topic 5 Data Abstraction Note: This represents a change in order. We are skipping to chapter 2 without finishing.
Fall 2008Programming Development Techniques 1 Topic 14 Multiple Representations of Abstract Data – Data Directed Programming and Additivity Section
מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real i 13 atan(2/3)
6.001 SICP 1/ SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 14: Object Oriented Programming 한 태숙.
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)
IMAGINARY NUMBERS AND DEMOIVRE’S THEOREM Dual 8.3.
1/38 Tagged Data Tag: a symbol in a data structure that identifies its type Why we need tags Extended example: evaluating arithmetic expressions.
מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections – 2.2.1)
CS61A Lecture Colleen Lewis. Clicker poll How do you think the midterm compared to previous midterms online? A)Harder B)A little harder.
SICP Tagged data Why do we need tags Concept of tags Extended example.
Abstraction A way of managing complexity for large programs A means of separating details of computation from use of computation Types of Abstraction Data.
Additional Scheme examples
Tagged Data Tag: a symbol in a data structure that identifies its type
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
6.001 Jeopardy.
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
6.001 SICP Object Oriented Programming
The role of abstractions
Complex Number Arithmetic
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Lecture 18 Infinite Streams and
Lecture 15: Tables and OOP
Dynamic Scoping Lazy Evaluation
Lecture #6 section pages pages72-77
Lecture #6 מבוא מורחב.
Lecture #8 מבוא מורחב.
Lecture 16: Tables and OOP
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
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 SICP Interpretation Parts of an interpreter
Lecture # , , , , מבוא מורחב.
Lecture 13: Assignment and the Environment Model (EM)
Presentation transcript:

מבוא מורחב 1 Lecture #13

מבוא מורחב 2 Multiple representations of data

מבוא מורחב 3 Complex numbers imaginary real i 13 atan(2/3)

מבוא מורחב 4 How do we have them both ? ( ' rectangular 3 2) ( ' polar (sqrt 13) (atan 2 3)) We tag the data:

מבוא מורחב 5 How do we have them both ? (define (attach-tag type-tag contents) (cons type-tag contents)) (define (type-tag datum) (if (pair? datum) (car datum) (error "Bad tagged datum -- TYPE-TAG" datum))) (define (contents datum) (if (pair? datum) (cdr datum) (error "Bad tagged datum -- CONTENTS" datum))) We tag the data:

מבוא מורחב 6 Data directed programming work directly with the table: types operations Polar Rectangular real-part imag-part magnitude angle real-part-polar imag-part-polar magnitude-polar angle-polar real-part-rectangular imag-part-rectangular magnitude-rectangular angle-rectangular

מבוא מורחב 7 Data-directed programming (Cont) Assume we have (put ) (get ) (put 'real-part '(rectangular) foo) => undef (get 'real-part '(rectangular)) => foo (put 'a '(a b c) foo1) => undef (get 'a '(a b c)) => foo1 will do them later in the course.

מבוא מורחב 8 Generic selectors (define (real-part z) (apply-generic 'real-part z)) (define (imag-part z) (apply-generic 'imag-part z)) (define (magnitude z) (apply-generic 'magnitude z)) (define (angle z) (apply-generic 'angle z)) (define (apply-generic op. args) (let ((type-tags (map type-tag args))) (let ((proc (get op type-tags))) (if proc (apply proc (map contents args)) (error "No method for these types -- APPLY-GENERIC" (list op type-tags))))))

מבוא מורחב 9 Example (real-part ’(rectangular 3 2)) (apply-generic ’real-part ’(rectangular 3 2)) (let ((type-tags (map type-tag ((’rectangular 3 2))))) (let ((proc (get ’real-part type-tags))) (if proc (apply proc (map contents ((’rectangular 3 2)))) (error...)))) (let ((proc (get ’real-part (’rectangular)))) (if proc (apply proc ((3 2))) (error...)))

10 Rectangular implementation (define (install-rectangular-package) ;; internal procedures (define (real-part z) (car z)) (define (imag-part z) (cdr z))... (define (make-from-mag-ang r a) (cons (* r (cos a)) (* r (sin a)))) ;; interface to the rest of the system (define (tag x) (attach-tag 'rectangular x)) (put 'real-part '(rectangular) real-part) (put 'imag-part '(rectangular) imag-part) (put 'magnitude '(rectangular) magnitude) (put 'angle '(rectangular) angle) (put 'make-from-real-imag 'rectangular (lambda (x y) (tag (make-from-real-imag x y)))) (put 'make-from-mag-ang 'rectangular (lambda (r a) (tag (make-from-mag-ang r a)))) 'done)

11 Polar implementation (define (install-polar-package) ;; internal procedures (define (magnitude z) (car z)) (define (angle z) (cdr z))... (define (make-from-real-imag x y) (cons (sqrt (+ (square x) (square y))) (atan y x))) ;; interface to the rest of the system (define (tag x) (attach-tag 'polar x)) (put 'real-part '(polar) real-part) (put 'imag-part '(polar) imag-part) (put 'magnitude '(polar) magnitude) (put 'angle '(polar) angle) (put 'make-from-real-imag 'polar (lambda (x y) (tag (make-from-real-imag x y)))) (put 'make-from-mag-ang 'polar (lambda (r a) (tag (make-from-mag-ang r a)))) 'done)

מבוא מורחב 12 Finally (define (make-from-real-imag x y) ((get 'make-from-real-imag 'rectangular) x y)) (define (make-from-mag-ang r a) ((get 'make-from-mag-ang 'polar) r a))

מבוא מורחב 13 Alternative -- message passing style Make “intelligent” data objects. decompose the table by column types operations Polar Rectangular real-part imag-part magnitude angle real-part-polar imag-part-polar magnitude-polar angle-polar real-part-rectangular imag-part-rectangular magnitude-rectangular angle-rectangular

מבוא מורחב 14 Message passing style (define (make-from-real-imag x y) (lambda (op) (cond ((eq? op 'real-part) x) ((eq? op 'imag-part) y) ((eq? op 'magnitude) (sqrt (+ (square x) (square y)))) ((eq? op 'angle) (atan y x)) (else (error... ))))) (define (real-part z) (z 'real-part)) (define (imag-part z) (z 'imag-part)) (define (magnitude z) (z 'magnitude)) (define (angle z) (z 'angle))

מבוא מורחב 15 Example (message passing) (define x (make-from-real-imag 3 2)) (define x (lambda (op) (cond ((eq? op 'real-part) 3) ((eq? op 'imag-part) 2) ((eq? op 'magnitude) (sqrt (+ (square 3) (square 2)))) ((eq? op 'angle) (atan 2 3)) (else (error... ))))) (real-part x) (x 'real-part) ==> 3

מבוא מורחב 16 Message passing style (define (make-from-mag-ang r a) (lambda (op) (cond ((eq? op 'real-part) (* r (cos a))) ((eq? op 'imag-part) (* r (sin a))) ((eq? op 'magnitude) r) ((eq? op 'angle) a) (else (error... )))))

מבוא מורחב 17 Another example for data directed prog.

18 Generic arithmetic operations add sub mul div add-rat sub-rat mul-rat div-rat add-complex sub-complex mul-complex div-complex real-part imag-part magnitude angle polar rectangular

מבוא מורחב 19 Generic arithmetic operations (’rational 1 2) (’complex ’rectangular 3 2) (’complex ’polar 2 2)

20 Generic arithmetic operations (define (add x y) (apply-generic 'add x y)) (define (sub x y) (apply-generic 'sub x y)) (define (mul x y) (apply-generic 'mul x y)) (define (div x y) (apply-generic 'div x y)) (add (’rational 3 4) (’rational 1 2)) (apply-generic 'add (’rational 3 4) (’rational 1 2)) (let ((type-tags (map type-tag ((’rat. 3 4) (’rat. 1 2))))) (let ((proc (get 'add type-tags))) (if proc (apply proc (map contents ((’rat. 3 4) (’rat. 1 2)))) (error... )))) (let ((proc (get 'add ((’rat. 3 4) (’rat. 1 2))))) (if proc (apply proc (map contents ((’rat. 3 4) (’rat. 1 2)))) (error... )))

21 Generic arithmetic operations (define (install-rational-package) ;; internal procedures (define (numer x) (car x)) (define (denom x) (cdr x)) (define (make-rat n d)...) (define (add-rat x y)...)... ;; interface to rest of the system (define (tag x) (attach-tag 'rational x)) (put 'add '(rational rational) (lambda (x y) (tag (add-rat x y)))) (put 'sub '(rational rational) (lambda (x y) (tag (sub-rat x y))))... (put 'make 'rational (lambda (n d) (tag (make-rat n d)))) 'done)

22 Generic arithmetic operations (define (install-complex-package) (define (make-from-real-imag x y) ((get 'make-from-real-imag 'rectangular) x y)) (define (make-from-mag-ang x y).. ) ;; internal procedures (define (add-complex z1 z2)...) (define (sub-complex z1 z2)...)... ;; interface to rest of the system (define (tag z) (attach-tag 'complex z)) (put 'add '(complex complex) (lambda (z1 z2) (tag (add-complex z1 z2)))) (put 'sub '(complex complex) (lambda (z1 z2) (tag (sub-complex z1 z2)))) (put 'make-from-real-imag 'complex (lambda (x y) (tag (make-from-real-imag x y)))) (put 'make-from-mag-ang 'complex ….) 'done)

23 Generic arithmetic operations (define (make-rational n d) ((get 'make 'rational) n d)) (define (make-complex-from-real-imag x y) ((get 'make-from-real-imag 'complex) x y)) (define (make-complex-from-mag-ang r a) ((get 'make-from-mag-ang 'complex) r a))