(load "file.scm") in the new environment in the diagram

The MIT circuit (load ...) procedure apparently takes in the environment as a parameter. Is there a way to β€œclone” the current environment and transfer it there so that I can isolate the file environment from my own?

(I looked here , but I did not find anything ...)

+4
source share
1 answer

How about something like that?

 (define (clone-env env) (let ((bindings (environment-bindings env))) (make-top-level-environment (map car bindings) (map cadr bindings)))) 1 ]=> (define foo 1) ;Value: foo 1 ]=> (eq? (the-environment) (clone-env (the-environment))) ;Value: #f 

Edited to add:

I'm not quite sure what you are trying to do, but here is what I did to check above. I created a foo.scm file containing:

 (set! foo 2) (define baz (+ foo foo)) baz 

Then

 1 ]=> (define foo 1) ;Value: foo 1 ]=> (load "foo.scm" (clone-env (the-environment))) ;Loading "foo.scm"... done ;Value: 4 1 ]=> foo ;Value: 1 1 ]=> baz ;Unbound variable: baz ;To continue, call RESTART with an option number: ; (RESTART 3) => Specify a value to use instead of baz. ; (RESTART 2) => Define baz to a given value. ; (RESTART 1) => Return to read-eval-print level 1. 2 error> 
+3
source

Source: https://habr.com/ru/post/1344047/


All Articles