Created
November 12, 2013 23:08
-
-
Save yamanetoshi/7440496 to your computer and use it in GitHub Desktop.
EoPL Ex.2.17
This file contains 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
(define empty-env | |
(lambda () | |
(lambda (sym search) | |
(if search | |
#f | |
(eopl:error 'apply-env "No binding for ~s" sym))))) | |
(define extend-env | |
(lambda (syms values env) | |
(lambda (sym search) | |
(let ((pos (list-find-position sym syms))) | |
(if search | |
(if (number? pos) | |
#t | |
(env sym #t)) | |
(if (number? pos) | |
(list-ref values pos) | |
(apply-env env sym))))))) | |
(define apply-env | |
(lambda (env sym) | |
(env sym #f))) | |
(define has-association? | |
(lambda (env sym) | |
(env sym #t))) | |
(define list-find-position | |
(lambda (sym los) | |
(list-index (lambda (sym1) (eqv? sym1 sym)) los))) | |
(define list-index | |
(lambda (pred ls) | |
(cond | |
((null? ls) #f) | |
((pred (car ls)) 0) | |
(else | |
(let ((list-index-r (list-index pred (cdr ls)))) | |
(if (number? list-index-r) | |
(+ list-index-r 1) | |
#f)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment