write-you-a-scheme-v2 / scheme

Write You a Scheme
https://www.wespiser.com/writings/wyas/home.html
MIT License
552 stars 114 forks source link

Repl doesn't persist definitions #22

Open lambdageek opened 7 years ago

lambdageek commented 7 years ago

The Repl doesn't remember definitions:

Repl> (define myid (lambda (x) x))
myid
Repl> (myid 1)
Error Unbound Variable: myid

This works:

Repl> (begin (define myid (lambda (x) x)) (myid 1))
1
lambdageek commented 7 years ago

The cute trick here would be to capture the interpreter state just after evaluating each expression in Eval.evalBody (crucially, including the extended environment) and to return to Repl.process both the result of evaluating the current expression, and a suspended computation to run on the /next/ expression.

timmyjose commented 2 years ago

Wow. This doesn't sound good. I tested the original WYAS code, and it works fine. I guess I'll use that project then.

adamwespiser commented 2 years ago

Yes, it's a bit unfortunate, I need to return to this problem when I get some time.

My thinking on the issue is that using ReaderT evaluator makes the tutorial a bit more understandable and accessible. For that, we lose a simple way to persist definitions: there's just no state to update!

For the evaluator, there must be a way to run eval, e -> a, capture that a, stick it in the e, then run the next line with the appended e.