CS220 Programming Principles 프로그래밍의 이해 2002 년 가을학기 Class 8 한 태숙.

Slides:



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

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:
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
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.
מבוא מורחב 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)
(define applicative-eval (lambda (exp) (cond ((atomic? exp) (eval-atomic exp)) ((special-form? exp) (eval-special-form exp)) ((list-form? exp) (eval-list.
מבוא מורחב 1 Lecture #13. מבוא מורחב 2 Multiple representations of data.
מבוא מורחב - שיעור 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.
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 / | \ \ / | \ \
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 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 한 태숙.
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 한 태숙.
Fall 2008Programming Development Techniques 1 Topic 20 Concurrency Section 3.4.
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)
Message Passing: Alternative to Generics data is “active” — knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=
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.
Operational Semantics of Scheme
Tagged Data Tag: a symbol in a data structure that identifies its type
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
6.001 SICP Object Oriented Programming
The interpreter.
The role of abstractions
Complex Number Arithmetic
6.001 SICP Data abstractions
The Metacircular Evaluator
Lecture 18 Infinite Streams and
Lecture 15: Tables and OOP
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Lecture #6 section pages pages72-77
Lecture #6 מבוא מורחב.
Lecture 16: Tables and OOP
6.001 SICP Further Variations on a Scheme
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
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Lecture #6 section pages pages72-77
6.001 SICP Variations on a Scheme
Lecture 14: The environment model (cont
Lecture 11: Multiple representations of abstract data Message passing Overloading Section 2.4, pages ,2.5.2 pages מבוא.
Lecture #7 מבוא מורחב.
Today’s topics Abstractions Procedural Data
6.001 SICP Interpretation Parts of an interpreter
Lecture 13: Assignment and the Environment Model (EM)
*Lecture based on notes from SICP
Presentation transcript:

CS220 Programming Principles 프로그래밍의 이해 2002 년 가을학기 Class 8 한 태숙

2 Strategies for Managing Data Complexity n Separate specification from implementation n Procedural interface to data (enables alternative representations) n Manifest typing (enables multiple representations) n Generic operations –Dispatch on type –Table-driven generic interface (Data-Directed Programming)

3 n Packages n Closure n Coercion n Message-Passing

4 Rectangular and Polar Coordinates n Data-abstraction barriers in complex-number +c, -c, *c, /c Complex-arithmetic package Rectangular representation Polar representation List structure and primitive machine arithmetic Programs that use complex numbers

5 Selector and Constructor n Constructor (make-rectangle (real-part z) (imag-part z)) (make-polar (magnitude z) (angle z)) n Selector real-part, imag-part magnitude, angle

6 Complex Numbers - Operators Complex Number Arithmetic (define (+c z1 z2) (make-rectangular (+ (real-part z1) (real-part z2)) (+ (imag-part z1) (imag-part z2)))) (define (-c z1 z2) (make-rectangular (- (real-part z1) (real-part z2)))) (- (imag-part z1) (imag-part z2)))) (define (*c z1 z2) (make-polar (* (magnitude z1) (magnitude z2)) (+ (angle z1) (angle z2)))) (define (/c z1 z2) (make-polar (/ (magnitude z1) (magnitude z2)) (- (angle z1) (angle z2))))

7 Dual Implementation(I) n Ben’s representation - rectangular coordinates (define (make-rectangular x y) (cons x y)) (define (real-part z) (car z)) (define (imag-part z) (cdr z)) (define (make-polar r a) (cons (* r (cos a)) (* r (sin a)))) (define (magnitude z) (sqrt (+ (square (car z))(square (cdr z))))) (define (angle z) (atan (cdr z) (car z)))

8 Dual Implementation(II) n Alyssa’s representation - Polar coordinates (define (make-rectangular x y) (cons (sqrt (+ (square x)(square y))) (atan y x))) (define (real-part z)(* (car z)(cos (cdr z)))) (define (imag-part z)(* (car z)(sin (cdr z)))) (define (make-polar r a) (cons r a)) (define (magnitude z) (car z)) (define (angle z) (cdr z))

9 Tagged Data n In order to support both types of complex numbers we need to be able to guarantee that, –selectors return appropriate values, independent of their implementation n We need TYPED data object, where the types can be seen by inspection. –Constructor - attach-tag –Selector - type-tag, contents –Predicate - is-the-type?

10 Type Tag - Implementation (define (attach-tag type-tag contents) (cons type-tag contents) (define (type-tag datum) (if (pair? datum) (car datum) (error “Bad datum --TYPE-TAG” datum))) (define (contents datum) (if (pair? datum) (cdr datum) (error “Bad datum --CONSTENS” datum)))

11 Complex Numbers - Rectangular? Polar? n Two types (representations) - rectangular and polar (define (rectangular? z) (eq? (type-tag z) ’rectangular)) (define (polar? z) (eq? (type-tag z) ’polar)) n We must add on a type tag whenever we construct complex numbers. n Since both representations coexist, we need to change the names of the procedures so as to avoid name conflicts.

12 Revised rectangular representation(I) ;Rectangular = ’rectangular x RepRect (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)))))

13 Revised rectangular representation(II) ; Rectangular -> Sch-Num (define (real-part-rectangular z) (car (contents z))) (define (imag-part-rectangular z) (cdr (contents 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)))

14 Revised polar representation(I) ;Polar = ’polar x RepPolar (define (make-from-mag-ang-polar r a) (attach-tag ’polar (cons r a))) (define (make-from-real-imag-polar x y) (attach-tag ’polar (cons (sqrt (+ (square x) (square y))) (atan y x))))

15 Revised polar representation(II) ; Polar -> Sch-Num (define (magnitude-polar z) (car (contents z))) (define (angle-polar z) (cdr (contents z))) (define (real-part-polar z) (* (magnitude-polar z)) (cos(angle-polar z)))) (define (imag-part-polar z) (* (magnitude-polar z) (sin(angle-polar z))))

16 Generic Operators and Constructors(I) ;; Complex = Rectangular U Polar ;; Selectors: Complex ->Sch-Num (define (real-part z) (cond ((rectangular? z) (real-part-rectangular z)) ((polar? z) (real-part-polar z)) (else (error “Unknown type-REAL-PART” z))))

17 Generic Operators and Constructors(II) ;; Complex = Rectangular U Polar ;; Selectors: Complex ->Sch-Num (define (imag-part z) (cond ((rectangular? z) (imag-part-rectangular z)) ((polar? z) (imag-part-polar z)) (else (error “Unknown type-IMAG-PART” z))))

18 Generic Operators and Constructors(III) ;; Complex = Rectangular U Polar ;; Selectors: Complex ->Sch-Num (define (magnitude z) (cond ((rectangular? z) (magnitude-rectangular z)) ((polar? z) (magnitude-polar z)) (else (error “Unknown type-MAGNITUDE” z))))

19 Generic Operators and Constructors(IV) ;; Complex = Rectangular U Polar ;; Selectors: Complex ->Sch-Num (define (angle z) (cond ((rectangular? z) (angle-rectangular z)) ((polar? z) (angle-polar z)) (else (error “Unknown type-ANGLE” z))))

20 Generic Operators and Constructors(V) ;; make-rectangular type Complex ;; Constructor: Sch-Num, Sch-Num -> Complex (define (make-rectangular x y) (make-from-real-imag-rectangular x y)) ;; make-polar type Complex ;; Constructor: Sch-Num, Sch-Num -> Complex (define (make-polar r a) (make-from-mag-angle-polar r a))

21 Generic Complex-Arithmetic System n Data-abstraction barriers in complex-number +c, -c, *c, /c Complex-arithmetic package Rectangular representation Polar representation List structure and primitive machine arithmetic Programs that use complex numbers real-part imag-part magnitude angle

22 Problems with this Approach n Generic Selectors depending on Type Tag –Dispatching on Type n Generic interface procedures must know about all the different representation. –need to add a clause for the new tag n No two procedures in the entire system have the same name. –can be programmed separately, but not independent n ==> Not Additive!

23 Data-Directed Programming n Table Driven Generic Interface Table of complex-number system Types Polar Rectangular real-part real-part-polar real-part-rectangular imag-part imag-part-polar imag-part-rectangular magnitude magnitude-polar magnitude-rectangular anlge angle-polar angle-rectangular Operations

24 Implementing Table Driven Operations n We need two table-manipulating operations and generic application of procedure. (put op type item) – installs the item in the table, indexed by the op and the type (get op type) –looks up the op, type entry in the table and returns the item found there. If no item is found, get returns false.

25 Ben’s Rectangular Package (p182) (define (install-rectangular-package) ;; internal procedures ;; RepRec = Sch-Num x Sch-Num (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))))

26 ;;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)

27 Alyssa’s Polar Package (p183) (define (install-polar-packaage) ;; internal procedures (define (magnitude z) (car z)) (define (angle z) (cdr z)) (define (make-from-mag-ang r a)(cons r a)) (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)))

28 ;;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)

29 General “operation” procedure n Apply-generic –applies a generic operation to some argument –searches table with type and operation and applies the resulting procedure if one is present (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- apply”)))))

30 Generic Selectors Using Apply-generic (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))

31 Constructors Exported ; export constructor outside of the package (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))

32 Examples to be traced (define my-z (make-from-real-imag 3 4)) (real-part my-z)

33 Message passing n Decompose table into columns n Make object as dispatch procedures –dispatch on operation names by typed object n A data object receives the requested operation name as a “message.”

34 Message passing (Example) (define (make-from-real-immag 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” op)))) dispatch) (define (apply-generic op arg) (arg op))

35 System with Generic Operations +c, -c, *c, /c Complex-arithmetic Rectangular representation Polar representation List structure and primitive machine arithmetic Programs that use numbers real-part imag-part magnitude angle + - * / add-rat sub-rat mul-rat div-rat Rational arithmetic Ordinary arithmetic add sub mul div Generic arithmetic package

36 Adding Other kinds of Numbers n Ordinary numbers: (define (make-scheme-number n) (attach-tag ’scheme-number n)) (define (scheme-number? n) (eq? (type-tag n) ’scheme-number)) (define (add-scheme-number n1 n2) (make-scheme-number (+ n1 n2))) (define (sub-scheme-number n1 n2) (make-scheme-number (- n1 n2))) and ditto for mul div

37 Generic add using dispatch on type n complex, rationals, ordinary number (define add x y) (cond ((and (complex? x) (complex? y)) (add-generic-complex (contents x) (contents y))) ((and (rational? x) (rational? y)) (add-rat (contents x)(contents y)) ((and (scheme-number? x) (scheme-number? y)) (add-scheme-number (contents x) (contents y)))))

38 Generic Arithmetic Operation n add –add-rat on rational numbers –add-complex on complex numbers..... (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))

39 Ordinary Numbers (p189) (defne (install-scheme-number-package) ;; Internal rep : RepNum = Sch-Num ;; internal procedures - just use scheme! ;; External rep: Scheme-Number = ;; ’scheme-number x RepNum (define (tag x) (attach-tag ’scheme-number x)) (put ’add ’(scheme-number scheme-number) (lambda (x y) (tag (+ x y)))) (put ’sub ’(scheme-number scheme-number) (lambda (x y) (tag (- x y)))) (put ’mul ’(scheme-number scheme-number) (lambda (x y) (tag (* x y))))

40 (put ’div ’(scheme-number scheme-number) (lambda (x y) (tag (/ x y)))) (put ’make ’scheme-number (lambda (x) (tag x))) ’done) ;; External constructor for ordinary numbers: ;; Sch-Num --> Number (define (make-scheme-number n) ((get ’make ’scheme-number) n))

41 Rational Number - generic number arith. (define (install-rational-package) ;;Internal rep: RepRat = Generic-Num x Generic-Num ;; internal procedure on RepRat (define (make-rat n d) (cons n d)) (define (numer x) (car x)) (define (denom x) (cdr x)) (define (add-rat x y) (make-rat (add (mul (numer x)(denom y)) (mul (denom x)(numer y))) (mul (denom x)(denom y)))) (define (sub-rat x y) (make-rat (sub (mul (numer x)(denom y)) (mul (denom x)(numer y))) (mul (denom x)(denom y))))

42 (define (mul-rat x y) (make-rat (mul (numer x)(numer y)) (mul (denom x)(denom y)))) (define (div-rat x y) (make-rat (mul (numer x)(denom y)) (mul (denom x)(numer y)))) ;; External rep: Rational = ’rational x RepRat (define (tag x) (attach-tag ’rational x)) (put ’make ’rational (lambda (n d) (tag (make-rat n d)))) (put ’add ’(rational rational) (lambda (x y) (tag (add-rat x y)))) (put ’sub ’(rational rational) (lambda (x y) (tag (sub-rat x y))))

43 (put ’mul ’(rational rational) (lambda (x y) (tag (mul-rat x y)))) (put ’div ’(rational rational) (lambda (x y) (tag (div-rat x y)))) ‘done) ;; External constructor interface (define (make-rational n d) ((get ’make ’rational) n d))

44 Complex Package (p191) (define (install-complex-package) ;; Internal Rep: RepComplex = Rectangular U Polar ;; import from rectangular and polar packaages (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)) ;; internal definitions.... (define (+c z1 z2) (make-from-real-imag (add (real-part z1)(real-part z2)) (add (imag-part z1)(imag-part z2))))

45 (define (-c z1 z2) (make-from-real-imag (sub (real-part z1)(real-part z2)) (sub (imag-part z1)(imag-part z2)))) (define (*c z1 z2) (make-from-mag-ang (mul (magnitude z1)(magnitude z2)) (add (angle z1)(angle z2)))) (define (/c z1 z2) (make-from-mag-ang (div (magnitude z1)(magnitude z2)) (sub (angle z1)(angle z2))))

46 ;; interface to rest of system - export to table (define (tag z) (attach-tag ’complex)) (put ’add ’(complex complex) (lambda (z1 z2) (tag (+c z1 z2)))) (put ’sub ’(complex complex) (lambda (z1 z2) (tag (-c z1 z2)))) (put ’mul ’(complex complex) (lambda (z1 z2) (tag (*c z1 z2)))) (put ’div ’(complex complex) (lambda (z1 z2) (tag (/c z1 z2)))) (put ’make-from-real-imag ’compex (lambda (x y) (tag (make-from-real-imag x y)))) (put ’make-from-mag-ang ’compex (lambda (r a) (tag (make-from-mag-ang r a)))) ’done)

47 ;; Exterrnal constructor interface (define (make-complex-from-real-imag x y) ((get ’make-from-real-imag ’compex) x y)) (define (make-complex-from-mag-ang r a) ((get ’make-from-mag-ang ’compex) r a)) complexrectangular34 3+4i

48 Generic add using dispatch on type n complex, rationals, ordinary number (define add x y) (cond ((and (complex? x) (complex? y)) (add-generic-complex (contents x) (contents y))) ((and (rational? x) (rational? y)) (add-rat (contents x)(contents y)) ((and (scheme-number? x) (scheme-number? y)) (add-scheme-number (contents x) (contents y)))))

49 Generic Operation in Generic operation? n Generic call to ADD, MUL extend our system to deal with arbitrarily complex data types. (define (+c z1 z2) (make-from-real-imag (ADD (real-part z1)(real-part z2)) (ADD (imag-part z1)(imag-part z2)))) (define (add-rat x y) (make-rat (ADD (MUL (numer x) (denom y)) (MUL (numer y) (denom x))) (MUL (denom x) (denom y))))

50 Example to be traced (define 2+5i (make-complex-from-real-imag (make-scheme-number 2) (make-scheme-number 5))) (define 4+6i (make-complex-from-real-imag (make-scheme-number 4) (make-scheme-number 6))) (define 1+3i (make-complex-from-real-imag (make-scheme-number 1) (make-scheme-number 3))) (define 2+3i (make-complex-from-real-imag (make-scheme-number 2) (make-scheme-number 3))) (define 2+5i/4+6i (make-rational 2+5i 4+6i) (define 1+3i/2+3i (make-rational 1+3i 2+3i)

51 Now try manipulating..... (add 2+5i/4+6i 1+3i/2+3i) (apply-generic ’add ’(rational (complex (rect (scheme-num 2)(scheme-num 5))) (complex (rect (scheme-num 4)(scheme-num 6)))) ’(rational (complex (rect (scheme-num 1)(scheme-num 3))) (complex (rect (scheme-num 2)(scheme-num 3)))) ==> applying ’add and ’(rational rational)

52 (attach-tag ’rational (add-rat ’((complex (rect (scheme-num 2)(scheme-num 5))) (complex (rect (scheme-num 4)(scheme-num 6)))) ’((complex (rect (scheme-num 1)(scheme-num 3))) (complex (rect (scheme-num 2)(scheme-num 3)))))).... (attach-tag ’rational (make-rat (add ’(complex (polar (scheme-num 19.42)(scheme-num 124.4))) (complex (polar(scheme-num 14.14)(scheme-num 135.0)))) ’(complex (polar (scheme-num 36.76)(scheme-num 112.6)))))

53 Combining Data of Different Types n adding complex and scheme-number? ;; to be included in the complex package (define (add-complex-to-schemenum z x) (make-from-real-imag (+ (real-part z) x) (imag-part z))) (put ’add ’(complex scheme-number) (lambda (z x) (tag (add-complex-to-schemenum z x)))) How may entries in the table???

54 Coercion n Forcing a data object of one type to convert to a data object of a different type: (define (scheme-number->complex n) (make-complex-from-real-imag (contents n) 0) Then install the procedure in a special coercion table indexed under the names of the two type (put-coercion ’scheme-number ’complex scheme-number->complex)

55 Revised Apply-general for coercion n See text-book page 196 n When does coercion work well, and when does it fail?