Thursday, June 4, 2009

Ex-2.3

Using message passing style of data abstraction here, so that procedures(which are the users of data abstraction make-rect) are independent of representation and will work even when the implementation is changed. In this style we make the data abstraction intelligent.
(define (perimeter rect)
(* 2
(+ (length rect) (breadth rect))))
(define (area rect)
(* (length rect) (breadth rect)))

;;universal selectors
(define (length rect)
(rect 'length))
(define (breadth rect)
(rect 'breadth))

;; representations..
;; a representation that takes length, and breadth of
;; the rectangle as input
(define (make-rect l b)
(define (dispatch msg)
(cond
((eq? msg 'length) l)
((eq? msg 'breadth) b)
(else (error "Message not recognized!"))))
dispatch)

If we think carefully, then the bean style of data abstraction we use in java when there are just getters/setters in the class implements this message passing style for data abstraction.

No comments:

Post a Comment