Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to reset user environment in Scheme REPL?

Scheme newbie question-

Is there a way for me to reset my current REPL environment (i.e. the default user environment) without quitting and restarting my REPL? Basically I'd like a way to wipe out my current environment so that none of my previous defines are in effect. This is using GNU/MIT Scheme.

If this is impossible, what's the best practice here when just messing around with code in the REPL? I've heard people talk about creating and deleting packages, but most examples seem to be for Common Lisp which is a bit different.

I did find information on how to do this in the Clojure REPL but there were caveats and it seems like it's Clojure-specific: Can I clean the repl?

Thanks!

Edit: I'm able to accomplish functionally the same thing by quitting and restarting the REPL process itself. I found a way to do this but keep the connection to my editor (vim) alive using vim-screen. This is an acceptable solution if there's no way to do it from within the REPL. However, I'll keep the question open a bit longer to see if there's a way to do this inside the language as I think it will be instructive.

like image 404
Eliot Avatar asked Aug 16 '11 05:08

Eliot


1 Answers

I think that this is implementation specific, but in MIT Scheme you can clear the REPL environment with:

1 ]=> (ge (make-top-level-environment))

The function (ge [environment]) "Changes the current REP loop environment to [environment]." and the function make-top-level-environment "returns a newly allocated top-level environment".

MIT Scheme has a bunch of environment-management functions that you can peruse here

I tested this on Mac OS X (10.6.7) with MIT Scheme 9.0.1 installed via the pre-built binary from the GNU site, with the following REPL session:

1 ]=> (define foo 1)

;Value: foo

1 ]=> foo

;Value: 1

1 ]=> (ge (make-top-level-environment))

;Value 13: #[environment 13]

1 ]=> foo

;Unbound variable: foo
;To continue, call RESTART with an option number:
; (RESTART 3) => Specify a value to use instead of foo.
; (RESTART 2) => Define foo to a given value.
; (RESTART 1) => Return to read-eval-print level 1.

2 error> 

I think that different implementations have different conventions but I don't think there's anything quite like Common Lisp's packages. If you're not wedded to MIT Scheme, you should check out Racket and Dr Racket, which is a nice IDE that might be more powerful than a plain REPL at the command line, and I think it has some kind of module system. Racket is its own dialect of Scheme, so depending on what you're doing, it might not be appropriate. (the default language module in Racket is not the same as MIT Scheme)

I've struggled with all this recently (past few months) when I went looking for a Scheme that could run the code from Lisp in Small Pieces, which has a bunch of weird macros. Gambit ended up being the best bet. If you don't have a need like this though, check out Racket.

like image 57
michiakig Avatar answered Nov 08 '22 17:11

michiakig