-
Notifications
You must be signed in to change notification settings - Fork 1
/
b+c.scm
40 lines (29 loc) · 1.02 KB
/
b+c.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
(define (new-instance class . parameters)
(apply class parameters))
(define (send message object . args)
(let ((method (object message)))
(cond ((procedure? method) (apply method args))
(else (error "send" "Error in method lookup " method)))))
(define (point x y)
(let ((x x)
(y y))
(define (getx) x)
(define (gety) y)
(define (add p)
(point
(+ x (send 'getx p))
(+ y (send 'gety p))))
(define (type-of) 'point)
(define (self message)
(cond ((eqv? message 'getx) getx)
((eqv? message 'gety) gety)
((eqv? message 'add) add)
((eqv? message 'type-of) type-of)
(else (error "point" "Undefined message" message))))
self))
(define p (new-instance point 2 3))
(format #t "p x: ~a y: ~a~%" (send 'getx p) (send 'gety p))
(define q (new-instance point 4 5))
(define p+q (send 'add p q))
(format #t "q x: ~a y: ~a~%" (send 'getx q) (send 'gety q))
(format #t "p+q x: ~a y: ~a~%" (send 'getx p+q) (send 'gety p+q))