Download presentation
Presentation is loading. Please wait.
1
מבוא מורחב - שיעור 12 1 Lecture 12 Data directed programming Message passing dotted-tail notation & apply Section 2.4, pages 169-187 2.5.1,2.5.2 pages 187-197 (but with a different example)
2
מבוא מורחב - שיעור 12 2 Multiple Representations of Abstract Data We develop a package for handling geometrical figures Each figure has a unique data representation Want to support Generic Operations on figures, such as: Compute figure area Compute figure circumference Print figure parameters, etc. Example: geometrical figures
3
מבוא מורחב - שיעור 12 3 Geometrical figures: interface We will now see 3 alternative implementations… Constructors: (make-rectangle width height) (make-circle radius) (make-square length) Rectangle implementation.. Circle implementation.. Square implementation.. Generic operations: (area figure) (circumference figure) (fig-display figure)..
4
מבוא מורחב - שיעור 12 4 Implementation #1: tagged data Main idea: add a tag (symbol) to every figure instance Generic procedures dispatch on parameter type ( ' circle 2) ( 'square 1 ) ( ' rectangle 1. 2)
5
מבוא מורחב - שיעור 12 5 Attaching tags. (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)))
6
מבוא מורחב - שיעור 12 6 Tagged data: rectangle implementation (define (make-rectangle width height) (attach-tag 'rectangle (cons width height))) (define (width rect) (car rect)) (define (height rect) (cdr rect)) (define (circumference-rect rect) (* 2 (+ (width rect) (height rect)))) (define (area-rect rect) (* (width rect) (height rect))) (define (fig-display-rect rect) (my-display "Rectangle: width, ” (width rect) ", height: " (height rect))) assume my-display takes any number of arguments and prints them.
7
מבוא מורחב - שיעור 12 7 Tagged data: circle implementation (define (make-circle radius) (attach-tag 'circle (list radius))) (define PI 3.1415926) (define (radius circle) (car circle)) (define (circumference-circle circle) (* 2 PI (radius circle))) (define (area-circle circle) (let ((r (radius circle))) (* PI r r))) (define (fig-display-circle circle) (my-display “Circle: radius, ” (radius circle)))
8
מבוא מורחב - שיעור 12 8 Tagged data: generic operations (define (circumference fig) (cond ((eq? 'rectangle (type-tag fig)) (circumference-rect (contents fig))) ((eq? 'circle (type-tag fig)) (circumference-circle (contents fig)..)) (define (area fig) (cond ((eq? 'rectange (type-tag fig)) (area-rect (contents fig))) ((eq? 'circle (type-tag fig)) (area-circle (contents fig)..)) (define (fig-display fig) (cond ((eq? 'rectange (type-tag fig)) (fig-display-rectangle (contents fig))) ((eq? 'circle (type-tag fig)) (fig-display-circle (contents fig)..)) Dispatch on type
9
מבוא מורחב - שיעור 12 9 The Difficulties The generic procedures must know about all types. If we want to add a new type we need to add it to each of the operations, be careful with name clashes. The system is not additive/modular ( define (area fig) (cond ((eq? 'rectange (type-tag fig)) (area-rect (contents fig))) ((eq? 'circle (type-tag fig)) (area-circle (contents fig)..))
10
מבוא מורחב - שיעור 12 10 Implementation #2: data directed programming Generic operations circumference area fig-display types Circle Rectangle circumference-circle area-circle fig-display-circle circumference-rect area-rect fig-display-rect Square circumference-square area-square fig-display-square Main idea: work with a table. Keep a pointer to the right procedure to call in the table, keyed by the operation/type combination
11
מבוא מורחב - שיעור 12 11 Data-directed programming (Cont) Assume we have a global two dimensional table and the following operations on it: (put key1 key2 val) Adds val to the table, under keys key1, key2 (get key1 key2) val/#f Retrieves the value found in the table under keys key1, key2 OR #f if the value is not found
12
מבוא מורחב - שיעור 12 12 Rectangle implementation (define (install-rectangle-package) ;; Implementation (define (width rect) (car rect)) (define (height rect) (cdr rect)) (define (circumference rect) (* 2 (+ (width rect) (height rect)))) (define (area rect) (* (width rect) (height rect))) (define (fig-display rect) (my-display "Rectangle: width,” (width rect) “, height " (height rect))) (define (make-rect width height) (attach-tag 'rectangle (cons width height))) ;; I nterface to the rest of the system (put 'circumference 'rectangle circumference) (put 'area 'rectangle area) (put 'fig-display 'rectangle fig-display) (put 'make-fig 'rectangle make-rect) 'done)
13
מבוא מורחב - שיעור 12 13 Circle implementation (define (install-circle-package) ;; Implementation (define PI 3.1415926) (define (radius circle) (car circle)) (define (circumference circle) (* 2 PI (radius circle))) (define (area circle) (let ((r (radius circle)) (* PI r r))) (define (fig-display circle) (my-display “Circle: radius,” (radius rect))) (define (make-circle radius) (attach-tag 'circle (list radius))) ;; I nterface to the rest of the system (put 'circumference 'circle circumference) (put 'area 'circle area) (put 'fig-display 'circle fig-display) (put 'make-fig 'circle make-circle) 'done)
14
מבוא מורחב - שיעור 12 14 Generic procedures (define (circumference fig) (apply-generic 'circumference fig)) (define (area fig) (apply-generic 'area fig)) (define (fig-display fig) (apply-generic 'fig-display fig)) Apply generic: Locates the right procedure in the table Calls it Passes the figure-data as a parameter
15
מבוא מורחב - שיעור 12 15 Apply-generic (define (apply-generic op arg) (let ((type (type-tag arg))) (let ((proc (get op type))) (if proc (proc (contents arg)) (error "No operation for this type -- APPLY-GENERIC" op type-tag))))) Get the type of the figure Locate the procedure that implements op on that type If there is such a procedure (get did not return #f) Call it and pass the figure-data as a parameter
16
מבוא מורחב - שיעור 12 16 Summary: Data Directed programming Data Directed programming: The data (argument) triggers the right operation based on the data type. Data Directed programming is more modular: 1.To add a representation, we only need to write a package for the new representation without changing generic procedures. (Execute the install procedure once). 2.Changes are local. 3.No name clashes. install-circle-package install-rectangle-package..
17
מבוא מורחב - שיעור 12 17 Implementation #3: Message Passing Data Directed: Intelligent operations that work with different data types. Message Passing: Intelligent data types that support different operations. In message passing the idea is that the data gets a message that tells it which operation to invoke, and returns the result. Main idea: the figure is represented by a procedure The procedure dispatches on the operation type
18
מבוא מורחב - שיעור 12 18 Message Passing (cont’) Message Passing: in each type we dispatch on operation. Data Directed: in each operation we dispatch on type. types Circle Rectangle circumference-circle area-circle fig-display-circle circumference-rect area-rect fig-display-rect Square circumference-square area-square fig-display-square Generic operations circumference area fig-display
19
מבוא מורחב - שיעור 12 19 Message passing style – rectangle figure (define (make-rectangle width height) (lambda (op) (cond ((eq? op 'area) (* height width)) ((eq? op 'circumference) (* 2 (+ width height))) ((eq? op 'fig-display) (my-display "Rectangle: width, ” width) “height, " height)) (else (error "Unknown op – MAKE-RECTANGLE" op))))) The constructor has it all. (define (make-rectangle width height) (define (dispatch op) (cond... ;; as above )) dispatch) Another way to write this:
20
מבוא מורחב - שיעור 12 20 Message passing style – circle figure (define (make-circle radius) (define PI 3.1415926) (lambda (op) (cond ((eq? op 'area) (* PI radius radius)) ((eq? op 'circumference) (* 2 PI radius)) ((eq? op 'fig-display) (my-display “Circle: radius ” radius)) (else (error "Unknown op – MAKE-CIRCLE" op))))) The constructor has it all.
21
מבוא מורחב - שיעור 12 21 Generic operations and new apply-generic (define (apply-generic op arg) (arg op)) (define (circumference fig) (apply-generic 'circumference fig)) (define (area fig) (apply-generic 'area fig)) (define (fig-display fig) (apply-generic 'make-fig fig))
22
מבוא מורחב - שיעור 12 22 Summary: Message Passing Additive Avoids name-clashes problems No need for a table More difficult to extend for multi-parameter operations Message passing is the basis for Object Oriented Programming and we will discuss it again later.
23
מבוא מורחב - שיעור 12 23 (define (proc x y. z) ) This dot signifies that proc accepts a variable number of parameters At least two parameters must be passed All other parameters will be passed in a list named ‘z’ Writing procedures with a variable number of parameters (dotted-tail notation)
24
מבוא מורחב - שיעור 12 24 Dotted-tail notation: examples (define (proc x y. z) ) (proc 1 2 3 4 5 6) x ? y ? z ? 1 2 (3 4 5 6) (proc 1 2) x ? y ? z ? 1 2 null (proc 1) ERROR
25
מבוא מורחב - שיעור 12 25 Dotted-tail notation: another example (define (proc. z) ) (proc 1 2 3 4 5 6) z ? (1 2 3 4 5 6) (proc) z ? null (proc 1 (list 2 3)) z ? (1 (2 3))
26
מבוא מורחב - שיעור 12 26 (count 1 7 8 9 1) 5 (count 0) 1 (count) 0 (define (count. s) _________________) Dotted-tail notation: another example We want to implement a procedure (count…) that gets a variable number of parameters and returns their number (length s)
27
מבוא מורחב - שיעור 12 27 (define (lt x. y) (length (filter (lambda (z) (< z x)) y))) What does this procedure do ? (lt 1 7 8 9 1 0 -1) 2 (lt 1000 1 10 0) 3 (lt 1) 0 (lt) ERROR Dotted-tail notation: yet another example lt returns the number of parameters that are smaller than the first parameter.
28
מבוא מורחב - שיעור 12 28 The builtin procedure Apply Suppose we want to apply some procedure proc but the parameters are available as a list (apply proc lst) will give the same result as assuming lst is (v1 v2 … vn) (proc v1 v2 … vn)
29
מבוא מורחב - שיעור 12 29 Apply examples (apply * (list 1 2 3)) (apply count (list 1 2 3)) (apply lt (list 10 1 2 3 11 9)) 6 3 4 (apply append '((1) (2) (3) (4))) (1 2 3 4) (apply expt '(2 4)) 16 (apply list '(1 2 3 4 5)) (1 2 3 4 5)
30
מבוא מורחב - שיעור 12 30 General map (define (f x y z) (+ x (* 2 y) (* 5 z))) (general-map f '(1 2 3) '(2 3 4) '(2 4 7)) (15 28 46) (define (general-map proc list1. other-lists) (if (null? list1) '() (let ((lists (cons list1 other-lists))) (let ((firsts (simple-map car lists)) (rests (simple-map cdr lists))) (cons (apply proc firsts) (apply general-map (cons proc rests))))))) We use the names simple-map and general-map for clarity. The general procedure is actually called map.
31
מבוא מורחב - שיעור 12 31 Another apply example Exercise: Implement a procedure, add-abs, that receives 0 or more numbers, and returns the sum of their absolute values. Examples: (add-abs) 0 (add-abs 1 -2 -3 4) 10 (define (add-abs. l) (if (null? l) 0 (+ (abs (car l)) _________________________))) (add-abs (cdr l)) Does not work!!
32
מבוא מורחב - שיעור 12 32 Another apply example Exercise: Implement a procedure, add-abs, that receives 0 or more numbers, and returns the sum of their absolute values. Examples: (add-abs) 0 (add-abs 1 -2 -3 4) 10 (define (add-abs. l) (if (null? l) 0 (+ (abs (car l)) _________________________))) (apply add-abs (cdr l)) This is ok!
33
מבוא מורחב - שיעור 12 33 Another apply example (cont) Exercise: Implement a procedure, add-abs, that receives 0 or more numbers, and returns the sum of their absolute values. Another solution (also using apply). (define (add-abs. l) (apply ________________________)) + (map abs l)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.