From: Barry Margolin
Subject: Re: having trouble evaluating function, help!
Date: 
Message-ID: <1g7vllINNot7@early-bird.think.com>
In article <·············@persimmon> ···@persimmon.ucsb.edu (David E. Goggin) writes:
>I am trying to write something like this.
>
>(defun wow (var test) (cond ((eval test) t) (t nil) )  )
>
>Where test is a expression like (= var 3)
...
>But I can`t seem to make it work right.  I get errors like
>"error in EVAL: test has no value"

Actually, the error should be "VAR has no value".

Assuming you're using Common Lisp or something similar, you can't do this
with EVAL.  Common Lisp uses lexical scoping rules, and EVAL evaluates the
expression in the global environment.  The variable VAR is only defined in
the local scope of the function WOW.

A good rule of thumb is that it shouldn't matter what the names of local
variables in a function are.  If recoding your function as

(defun wow (arg1 arg2) (cond ((eval arg2) t) (t nil)))

would affect anything, then you've done something wrong.

Perhaps if you could explain why you think you need to do this, we could
show you appropriate ways to write it.  Most of the time, if you think you
need to use EVAL to do something, you're wrong.

Actually, I lied above.  You can do this by making VAR a "special"
variable, which causes the binding to be done in the dynamic environment
rather than the lexical environment:

(defun wow (var test)
  (declare (special var))
  (cond ((eval test) t)
        (t nil)))

However, this is generally considered bad style.  Special variables are
normally used for environmental parameters (e.g. *PRINT-BASE*).
-- 
Barry Margolin
System Manager, Thinking Machines Corp.

······@think.com          {uunet,harvard}!think!barmar