6 ms·
An environment is basically a mapping of variables to their values. Its relative to a certain point in time of you program execution. So if you can (legally) r
by lukesandberg 16y ago
An environment is basically a mapping of variables to their values. Its relative to a certain point in time of you program execution. So if you can (legally) reference a variable at some point in your scheme program the it must be part of your environment.
A scope is relative to a variable. (i.e. the variables scope). It is essentially the set of program locations in which that variable is in the environment.
A closure is just an environment plus a lambda. So that when the lambda executes it still has access to the environment in which it was created (aka all that environments variables are still in scope). You can use closures to essentially extend the scope of variables so you can access them later.
A continuation is a little trickier. Basically a continuation is more about control flow than visibility. A continuation is what happens after a lambda returns. In most languages continuations are implictly managed by the program stack (and the return keyword). So you don't even think its anything special. But in scheme there is special support to help reify the continuation concept and make it available to the programmer.
I hope I didn't make things any more confusing.
- lukesandberg 16y agoWith concepts like these it can sometimes be difficult to form an appropriate mental model of how the machine is actually working (aka the implementations of the algorithm). So if you were to implement a scheme interpreter heres how i would represent these things. scope: map from symbols to objects. environment: linked list of scopes, where each scope is linked to its parent scope. (so to look up a variable value you just walk up the scope chain looking for where the symbol is defined). closure: pair of environment and lambda. where the environment is the one in which the lambda was defined. (so to execute a closure you just link the lambda scope to the given environment and then run the lambda) continuation: just a closure. continuations would most likely be implemented by rewriting the code after the method call as a lambda and passing the closure as the continuation. if you really want to see how the bits fit together you can read this blog series: http://michaux.ca/index#Scheme%20from%20Scratch http://michaux.ca/index#Scheme%20from%20Scratch and check out the corresponding github implementations: https://github.com/petermichaux/bootstrap-scheme https://github.com/petermichaux/bootstrap-scheme https://github.com/lukesandberg/bootstrap-scheme https://github.com/lukesandberg/bootstrap-scheme (shameless plug for my implementation)