Object-oriented programming in Scheme - slide 5 : 11 |
Due to (1) the first class status of functions, and due to (2) the use of static binding of free names, it is possible to interpret a closure as an object
With this interpretation, it is possible to regard certain function definitions as classes
(define (point x y) (letrec ((getx (lambda () x)) (gety (lambda () y)) (add (lambda (p) (point (+ x (send 'getx p)) (+ y (send 'gety p))))) (type-of (lambda () 'point)) ) (lambda (message) (cond ((eq? message 'getx) getx) ((eq? message 'gety) gety) ((eq? message 'add) add) ((eq? message 'type-of) type-of) (else (error "Message not understood"))))))