-
Notifications
You must be signed in to change notification settings - Fork 1
/
env1.scm
59 lines (51 loc) · 1.84 KB
/
env1.scm
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
;;; <---- START : env1.scm ---->
;;;+--------------------------------------------------------------------------------------------+ |
;;;| |
;;;| IMPEMENTATION: |
;;;| - Simple enviorment using list of 2-lists ADT |
;;;| - Each enviornment variable is a 2 element list |
;;;| - First elemnent of the list is name of var |
;;;| - Second element of the list is value of var |
;;;| |
;;;| NOTES: |
;;;| + loading the file "helpfulADT1.scm" to acces helpful functions for ADT described above.|
;;;+--------------------------------------------------------------------------------------------+
(load "helpfulADT1.scm")
;;; <---- Start of (make-empty-env): Returns an empty list since the enviorment is empty
(define make-empty-env
(lambda ()
'()
)
)
;;; End of (make-empty env) ---->
;;; <---- Start of (apply-env env v): Returns the value of v if in the given enviorment [env] else returns an error message
(define apply-env
(lambda (env v)
(cond
((null? env) ; If env is empty then no variablles exist for the name v
(error "apply-env: empty enviorment")
)
((equal? (car (car env)) v) ; First element of the first list is v [matching var found in env]
(car (cdr (car env))) ; Second element of that list is value of v
)
(else ; Else look for v in the rest of the env list
(apply-env (cdr env) v)
)
)
)
)
;;; End of (apply-env env v) ---->
;;; <---- Start of (extend-env v val env): Returns a new enviornment that has env (list of lists) appended with the list (v val). If v already has a value then replace it with val
(define extend-env
(lambda (v val env)
(cond
((in-env? v env)
(update-env v val env)
)
(else ; Add to env the 2 element list (v val)
(append env (list (list v val)))
)
)
)
)
;;; End of (extend-env v val env)