schememit-scheme

How do I define a sub environment in scheme?


I am just hacking around with Scheme (mit-scheme) and I have just figured out how you change the environment, so that '+' becomes a symbol for the equivalent procedure of the '-' operator.

Example

(environment-define user-initial-environment '+ -)
(eval (+ 3 2) user-initial-environment)
=> 1

I was just wondering if there were a simple way to deal with environments as variables so when I input an environment into eval, like so

(eval <exp> user-initial-environment) 

I don't have to use 'user-initial-environment'. So I can 'play' with different environments for a function.

(eval <exp> env) 

Where env is some predefined environment attached to my variable 'env'.


Solution

  • The relevant MIT Scheme documentation page on top-level environments could be instructive -- you can either extend an existing top-level environment (with extend-top-level-environment) or make a new one from scratch (with make-top-level-environment).

    For evaluating anything but the most trivial expressions, though, it might be instructive to extend either system-global-environment or user-initial-environment (cf 13.2: Environment Variables)