Created
April 26, 2011 21:30
-
-
Save bishboria/943213 to your computer and use it in GitHub Desktop.
OO in Scheme
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; All the OO stuff | |
(define (object selector-names . all-selectors) | |
(define (apply-it selector selector-names selectors parameters) | |
(cond ((null? selectors) '(I cant do that Dave!)) | |
((equal? selector (car selector-names)) (apply (car selectors) parameters)) | |
(#t (apply-it selector (cdr selector-names) (cdr selectors) parameters)))) | |
(lambda (selector . parameters) | |
(apply-it selector selector-names all-selectors parameters))) | |
;; Defining a new object type | |
(define (person name age) | |
(object '(name age) | |
(lambda () | |
name) | |
(lambda () | |
age))) | |
;; Usage | |
(define someone (person 'stu 28)) | |
(someone 'name) | |
(someone 'age) | |
(define (maths) | |
(object '(add) | |
(lambda (a b) | |
(+ a b)))) | |
((maths) 'add 3 5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment