On 5/8/07, Chicken Monk <[EMAIL PROTECTED]> wrote:
You'll have to pardon my ignorance... I am used to Python, where "eval"
and "exec" have access to all the variables that regular code has, even
in nested scopes.  So I was wondering if something similar could be done
in Scheme.

Here's a cheap-and-dirty way to do it; it's not perfect (especially
because there's some redundant typing involved) but you can have it
today:

(define-macro (eval-with-locals locals expr)
 `((eval '(lambda ,locals ,(eval expr))) ,@locals))

As an example:

(define z 1000)
(let ((x 10)
     (y 100))
 (eval-with-locals (x y) '(+ x y z)))
==> 1110

This uses (eval) to create an anonymous function that takes our
"locals" list as arguments. Then we call the anonymous function with
the actual values of the specified local values.

Actually, the redundancy of typing (x y) isn't a bad thing; there's no
guesswork involved, and no chance that you're accidentally binding to,
e.g., some global value of x.

Cheers,
Graham


_______________________________________________
Chicken-users mailing list
Chicken-users@nongnu.org
http://lists.nongnu.org/mailman/listinfo/chicken-users

Reply via email to