מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real 3 2 3 + 2i 13 atan(2/3)

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
데이타를 여러방식으로 구현할 때 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.
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 תרגול 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:
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Dotted tail notation (define (proc m1 m2. opt) ) Mandatory Arguments: m1, m2 Mandatory Arguments: m1, m2.
מבוא מורחב 1 Lecture #13. מבוא מורחב 2 Multiple representations of data.
מבוא מורחב - שיעור 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.
( (lambda (z) (define x (lambda (x) (lambda (y z) (y x)))) ( ( (x (lambda () z)) (lambda (z) z) 3 ) ) ) 2)
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7. Outline Symbols Data Directed Programming, Message Passing.
מבוא מורחב 1 Lecture #8. מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list )) ==> ( ) (scramble.
Quiz: Box and Pointer fun! (cons (cons (cons ‘hey (cons ‘there nil)) nil) (cons ‘wow nil)) (list ‘boo (append (list ‘hoo ‘hoo) (cons ‘see ‘me)))
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
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
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 한 태숙.
Fall 2008Programming Development Techniques 1 Topic 8 Sequences as Conventional Interfaces Section October 2008.
1 Data Abstraction. Pairs and Lists. (SICP Sections – 2.2.1)
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.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Outline 1.The special form quote 2.Data abstraction: Trie 3.Alternative list: Triplets 4.Accumulate-n.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 6. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
1/38 Tagged Data Tag: a symbol in a data structure that identifies its type Why we need tags Extended example: evaluating arithmetic expressions.
מבוא מורחב 1 Representing lists as binary trees
מבוא מורחב שיעור 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.
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)
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
6.001 SICP Object Oriented Programming
PPL Lazy Lists.
The role of abstractions
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
6.001 SICP Data abstractions
The Metacircular Evaluator
Lecture 15: Tables and OOP
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 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 Multiple representations of data

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

מבוא מורחב 3 Complex numbers arithmetic a + bi + (c + di) = a+c + (b+d)i r1e (i  1) * r2e (i  2) = (r1r2)e (i(  1+  2))

מבוא מורחב 4 High level usage (define (add-complex z1 z2) (make-from-real-imag (+ (real-part z1) (real-part z2)) (+ (imag-part z1) (imag-part z2)))) (define (sub-complex z1 z2) (make-from-real-imag (- (real-part z1) (real-part z2)) (- (imag-part z1) (imag-part z2)))) (define (mul-complex z1 z2) (make-from-mag-ang (* (magnitude z1) (magnitude z2)) (+ (angle z1) (angle z2)))) (define (div-complex z1 z2) (make-from-mag-ang (/ (magnitude z1) (magnitude z2)) (- (angle z1) (angle z2))))

מבוא מורחב 5 המימוש שמדמיין אריאל (define (real-part z) (car z)) (define (imag-part z) (cdr z)) (define (make-from-real-imag x y) (cons x y)) (define (magnitude z) (sqrt (+ (square (real-part z)) (square (imag-part z))))) (define (angle z) (atan (imag-part z) (real-part z))) (define (make-from-mag-ang r a) (cons (* r (cos a)) (* r (sin a))))

מבוא מורחב 6 המימוש מהזווית של מני (define (make-from-mag-ang r a) (cons r a)) (define (magnitude z) (car z)) (define (angle z) (cdr z)) (define (real-part z) (* (magnitude z) (cos (angle z)))) (define (imag-part z) (* (magnitude z) (sin (angle z)))) (define (make-from-real-imag x y) (cons (sqrt (+ (square x) (square y))) (atan y x)))

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

מבוא מורחב 8 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:

מבוא מורחב 9 Complex numbers: having both reps. (define (rectangular? z) (eq? (type-tag z) 'rectangular)) (define (polar? z) (eq? (type-tag z) 'polar)) (define (real-part z) (cond ((rectangular? z) (real-part-rectangular (contents z))) ((polar? z) (real-part-polar (contents z))) (else (error "Unknown type -- REAL-PART" z))))

מבוא מורחב 10 Complex numbers: having both reps. (define (magnitude z) (cond ((rectangular? z) (magnitude-rectangular (contents z))) ((polar? z) (magnitude-polar (contents z))) (else (error "Unknown type -- MAGNITUDE" z)))) angle, imag-part are similar

מבוא מורחב 11 and we have to rename (define (real-part-rectangular z) (car z)) (define (imag-part-rectangular z) (cdr z)) (define (magnitude-rectangular z) (sqrt (+ (square (real-part-rectangular z)) (square (imag-part-rectangular z))))) (define (angle-rectangular z) (atan (imag-part-rectangular z) (real-part-rectangular z))) (define (make-from-real-imag-rectangular x y) (attach-tag 'rectangular (cons x y))) (define (make-from-mag-ang-rectangular r a) (attach-tag 'rectangular (cons (* r (cos a)) (* r (sin a)))))

מבוא מורחב 12 and we have to rename (define (real-part-polar z) (* (magnitude-polar z) (cos (angle-polar z)))) (define (imag-part-polar z) (* (magnitude-polar z) (sin (angle-polar z)))) (define (magnitude-polar z) (car z)) (define (angle-polar z) (cdr z)) (define (make-from-real-imag-polar x y) (attach-tag 'polar (cons (sqrt (+ (square x) (square y))) (atan y x)))) (define (make-from-mag-ang-polar r a) (attach-tag 'polar (cons r a)))

מבוא מורחב 13 Constructors (define (make-from-real-imag x y) (make-from-real-imag-rectangular x y)) (define (make-from-mag-ang r a) (make-from-mag-ang-polar r a))

מבוא מורחב 14 add-complex, sub-complex real-part imag-part magnitude angle polar representation Rectangular representation

מבוא מורחב 15 Difficulties ? The generic selectors must know about all reps. Bureacracy (define (real-part z) (cond ((rectangular? z) (real-part-rectangular (contents z))) ((polar? z) (real-part-polar (contents z))) (else (error "Unknown type -- REAL-PART" z)))) Care about name clashes ==> Implementation is not additive Care about when there is a tag and when there isn’t

מבוא מורחב 16 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

מבוא מורחב 17 Data-directed programming (Cont) Assume we have (put ) (get ) will do them later in the course.

מבוא מורחב 18 Data-directed programming (Cont) Assume we have (put ) (get ) will do them later in the course.

19 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)

20 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)

מבוא מורחב 21 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))

מבוא מורחב 22 apply-generic (simple version) (define (simple-apply-generic op arg) (let ((type-tag (type-tag arg))) (let ((proc (get op (list type-tag)))) (if proc (proc (contents arg)) (error "No method for these types -- APPLY-GENERIC" (list op type-tag))))))

מבוא מורחב 23 apply-generic (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)))))) (apply + (list )) ==> 10

מבוא מורחב 24 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))

מבוא מורחב 25 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

מבוא מורחב 26 Message passing style (define (make-from-real-imag x y) (define (dispatch 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 "Unknown op -- MAKE-FROM-REAL-IMAG" op)))) dispatch) (define (apply-generic op arg) (arg op))

מבוא מורחב 27 Midterm last spring ממש / י שגרה (add-ind init lst) שמקבלת כקלט אינדקס התחלתי init ורשימה lst ומחזירה רשימה שאיבריה הם זוגות. האיברים הראשונים בזוגות הם אינדקסים רצים שמתחילים ב -init. האיברים השניים בזוגות הם איברי הרשימה lst. לדוגמא : (add-ind 0 (list 5 2 7)) ==> ((0. 5) (1. 2) (2. 7)) (define (add-ind init lst) (if (null? lst) nil (cons (cons init (car lst)) (add-ind (+ 1 init) (cdr lst)))))

מבוא מורחב 28 Midterm last spring (cont) השלם / י את מימוש השגרה (max-ind lst-ind) שמקבלת רשימה שהתקבלה מהפעלת add-ind על רשימת מספרים, ומחזירה את הזוג שבו האיבר השני הוא הגדול ביותר. לדוגמא : (max-ind (add-ind 0 (list ))) ==> (1. 17) (define (max-ind lst-ind) (accumulate ________________________________ ____________ )) lst-ind (lambda (x y) (if (> (cdr x) (cdr y)) x y)) (car lst-ind)

מבוא מורחב 29 Midterm last spring (Cont) תהא lst רשימת מספרים שכל איבריה שונים. הגדר שגרה (break-list lst) שמחזירה רשימה באורך שלוש. האיבר הראשון ברשימה שמוחזרת היא רשימת איברי lst שמופיעים לפני האיבר המירבי של lst. האיבר השני ברשימה הוא האיבר המירבי ב -lst. האיבר השלישי הוא רשימת איברי lst שמופיעים לאחר האיבר המירבי. לדוגמא : (break-list ‘( ))  ( ( ) 9 (2 5 7) ) (break-list ‘( ))  ( () 5 ( ) ) (break-list ‘(7))  ( () 7 () )

מבוא מורחב 30 Midterm last spring (Cont) (define (break-lst lst) (let ((lst-ind (add-ind lst))) (let ((maxi (max-ind lst-ind))) (list (map ______________________________ (filter _______________________________ lst-ind)) ____________ (map ______________________________ (filter _______________________________ lst-ind)) )))) (cdr maxi) cdr (lambda (x) (< (car x) (car maxi))) (lambda (x) (> (car x) (car maxi)))