[plt-scheme] Question about eval and environments

From: Chongkai Zhu (czhu at cs.utah.edu)
Date: Thu Oct 25 12:09:17 EDT 2007

Mike J. Bell wrote:
> I don't think this is PLT specific, so I apologize if this should be 
> on a more general scheme list.
> I'm interested in adding a generic hook to evaluate a piece of code at 
> an arbitrary point in my program.  This would be useful for debugging 
> purposes.  For instance:
> (let ((x 5))
>   ;(do a bunch of stuff with x)
>   (write (eval (read))))
> Obviously this isn't very useful outside of a loop or other more 
> complicated idea, but this is just a toy to show the problem.  When 
> (read) runs, I would like to type in something like:
> (set! x 7)
> This doesn't work.  The error is that 'x' is unbound.  So I tried:
> ...
>   (write (eval (read) (interaction-environment))))
> and that didn't work either.  It seems like I can't get to the 
> environment that contains the lexical closure created by the initial 
> (let...).
> Is there any way around this?  I supposed I could write my own (mini) 
> version of eval, but that seems pretty extreme.  How can I get access 
> to the lexical environment at the point (eval) is executed so I can 
> inject arbitrary code?

In short, you can't. So try some other way to do the debug, say using 
the debug tool.

> Thanks for any tips!!!
> Mike
> -- 
> Mike J. Bell on gmail
> ------------------------------------------------------------------------
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme

Posted on the users mailing list.