[plt-scheme] Re: Executing code created on the fly

From: Eli Barzilay (eli at barzilay.org)
Date: Sat Jul 26 22:52:37 EDT 2008

On Jul 26, Ihe wrote:
> On Jul 26, 7:57 pm, Eli Barzilay <e... at barzilay.org> wrote:
> > On Jul 26, Ihe wrote:
> >
> > > I tried doing this with eval and ran into problems. I am also having
> > > problems using macros or syntax rules e.g
> >
> > > (define-syntax make-program
> > >   (syntax-rules ()
> > >     [(make-program expr) (lambda (x y) expr)]))
> >
> > > I want to be able to say
> > > (let ((prog-body blah))
> > >     (make-program prog-body))
> >
> > > and have a different procedure returned depending on the value prog-
> > > body is set to.
> >
> > > Alas this does not work.
> >
> > Of course it doesn't -- `blah' in the above will be evaluated like any
> > other value -- the use of `make-program' makes it just as if you
> > wrote:
> >
> >   (let ((prog-body blah))
> >     (lambda (x y) prog-body))
> >
> > > Basically I want to be able to execute these programs created on
> > > the fly and to be able to capture the source of each program
> > > ... I don't really care how it is done (except not with eval due
> > > to problems already experienced).
> >
> > Are you sure you need `eval'?  If you do, then you'll need to set
> > up a proper namespace for it -- the default namespace is empty
> > (and that's probably causing the problems you're talking about).
> 
> I am posting to ask for a method that doesn't use eval.

The question still holds: you want *some* way to evaluate an
expression dynamically, `eval' *is* really the best choice for that,
but it's usually abused for things where it's not needed.  In other
words, the thing that people warn against is not using the name
`eval', its the fact that you evaluate code dynamically, no matter how
you actually do it.

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.