I was looking to create something similar to an object or an environment kind of like in OOP
This is what I was thinking:
(define env (variable 'x 10 env))
Where I define an env and create a variable of value 10 in that environment.
I also want to be able to call on the values in that environment. For example
(get-value env 'x)
> 10
The most I can understand is that it involves closures
but i'm not sure where to start from there
The easiest way of achieving this is using alist. The above
variable
andget-value
can be defined as the following:The initial environment is
'()
if you want to hide the empty list you can also define like this:There are many ways to do this. The classical way is to use an alist:
Al Petrofsky made eiod (Eval in one define) which did it like this:
In SICP you have frames with bindings:
Note that unlike the other ones that adds one this adds a whole set. eg. to bind
x
to5
andy
to7
you do:I'll add my own take on this:
The whole point by this answer is that given you have the design choice of both the
get-value
,variable
andthe-empty-environment
. As long as you can implement these it really doesn't matter how it is done. you can replace one for the other and the interpreter will still work. That is except the SICP one that requires you to do one frame at a time.