Skip to content

Instantly share code, notes, and snippets.

@chelseatroy
Last active November 2, 2019 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chelseatroy/c000098b09da90c2b0aee886f84120cb to your computer and use it in GitHub Desktop.
Save chelseatroy/c000098b09da90c2b0aee886f84120cb to your computer and use it in GitHub Desktop.
Types as Dispatch Procedures
(define (make-bob-box x y width height)
(define (dispatch message)
(cond ((eq? message 'width) width)
((eq? message 'height) height)
((eq? message 'type) 'bob-box)
(else (error "Bad method"))))
dispatch)
(define (make-alice-box x1 y1 x2 y2)
(define (dispatch message)
(cond ((eq? message 'width) (abs(- x2 x1)))
((eq? message 'height) (abs(- y2 y1)))
((eq? message 'type) 'alice-box)
(else (error "Bad method"))))
dispatch)
(define a (make-alice-box 1 2 3 4))
(define b (make-bob-box 1 2 3 4))
(b 'width) ;---> 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment