chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Re: eval and local variables


From: Graham Fawcett
Subject: Re: [Chicken-users] Re: eval and local variables
Date: Tue, 8 May 2007 13:31:52 -0400

On 5/8/07, Chicken Monk <address@hidden> 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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]