Last active
December 17, 2015 11:39
-
-
Save boxnos/5604200 to your computer and use it in GitHub Desktop.
プロトタイプチェーンの試作版。Racket用。第5版。 4版でバグ修正。
5版はalist->malistの余計な処理をprototype側に移動。
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
(require compatibility/mlist) | |
(define (alist->malist alist) | |
(list->mlist (map (lambda (i) (mcons (car i) (cdr i))) alist))) | |
(define (prototype malist) | |
(set! malist (mlist (mcons 'prototype malist))) | |
(define (search malist name t f) | |
(let ((res (massq name malist))) | |
(if (mpair? res) (t res) (f)))) | |
(lambda (name . value) | |
(if (null? value) | |
(letrec ((chain (lambda (l) | |
(search l name | |
(lambda (res) (mcdr res)) | |
(lambda () | |
(search l 'prototype | |
(lambda (res) (chain (mcdr res))) | |
(lambda () (error "undefind " . (car value))))))))) | |
(chain malist)) | |
(search malist name | |
(lambda (res) (set-mcdr! res (car value))) | |
(lambda () (set! malist (mcons (mcons name (car value)) malist))))))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment