[plt-scheme] 'eval' function seems to completely ignore scopingrules.

From: Anton van Straaten (anton at appsolutions.com)
Date: Sun Jan 20 18:14:25 EST 2008

> Do I understand this right: in scheme, code <> data? 

Data in one compilation phase is still equal to code in another.  For 
example, during the macro expansion phase for a macro system like 
syntax-case, you can use data to generate code.

What has changed since the early pre-CL days of Lisp is that there is 
now a clear distinction between phases.

> And in, say, CL, it still is =?

No, it isn't.  E.g. in SBCL:

(let ((y 4)) (eval 'y))

...produces an error, "variable y is unbound".

CL faces the same forces that Scheme does in this area.

> Where (online) can I read more about the background that lead to the 
> consensus "eval is not needed", "code doesn't need to equal data", "eval 
> can be replaced by X, Y, Z"? 

Try googling for the phrase "eval is evil".  You'll notice the 
Javascript and Python communities also mentioning this issue.

(Judging by Google, Ruby folk appear not to be as concerned - I predict 
that the Great Server Farm Meltdown of 2009 will change all that.)

> And what are the X, Y, Z?

It depends on what the problem is.

However, proper lexical implementation of lambda itself, first done in 
Scheme, is one of the things which can replace some uses of eval. 
Lambdas are constructors that are evaluated at runtime (modulo 
optimizations), and produce functions.  This is often missed by people 
focusing on compiling from "data" to code at runtime: Scheme gives you 
the ability to generate code from other code at runtime, which reduces 
the need to produce code from data.

Macros are another alternative in some cases.

Writing specific code which achieves what you're trying to achieve with 
eval is another alternative.  Such code usually provides a much more 
precise, secure, efficient, and tractable solution.

And it's still possible to compile and load code at runtime.  However, 
there tend to be constraints on this.  One typical constraint is that 
such code can't automatically access lexical variables in the 
environment in which it was compiled.  Of course, if you really need 
this you can use things like namespaces in PLT Scheme to communicate 
information to a piece of eval'd code.  At least, I assume you can; I've 
never needed to do that.


Posted on the users mailing list.