forked from alirezadaghigh99/PL-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.rkt
30 lines (24 loc) · 871 Bytes
/
environment.rkt
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
#lang racket
(define (empty-env) (list 'empty-env))
(define (extend-env var val env) (list 'extend-env var val env))
(define (apply-env env search-var)
(cond
[(eqv? (car env) 'empty-env)
(report-no-binding-found search-var)]
[(eqv? (car env) 'extend-env)
(let ((saved-var (cadr env))
(saved-val (caddr env))
(saved-env (cadddr env)))
(if (string=? search-var saved-var)
saved-val
(apply-env saved-env search-var)))]
[else (report-invalid-env env)]
))
(define report-no-binding-found
(lambda (search-var)
(error 'apply-env "No binding for ~s" search-var)))
(define report-invalid-env
(lambda (env)
(error 'apply-env "Bad environment: ~s" env)))
(define env '(extend-env a 6 (extend-env b 8 (extend-env c -4 (empty-env)))))
(provide (all-defined-out))