From: KanZen
Subject: Newbie Q: How can I evaluate a form?
Date: 
Message-ID: <10eb079f.0311181146.7f739ad1@posting.google.com>
Suppose I have some function and in the body I want to evaluate a
given form. It seems that I can do it with apply like this:

(defun some-function (some-form)
  (apply (first some-form) (rest some-form)))

(some-function '(+ 2 2))

This seems a little clumsy to me... is there some Common Lisp function like:
(evaluate-form some-form) or does no such thing exist?

From: Howard Ding <······@hading.dnsalias.com>
Subject: Re: Newbie Q: How can I evaluate a form?
Date: 
Message-ID: <m38ymdzc80.fsf@frisell.localdomain>
······@mail.com (KanZen) writes:


> This seems a little clumsy to me... is there some Common Lisp function like:
> (evaluate-form some-form) or does no such thing exist?

eval

-- 
Howard Ding
<······@hading.dnsalias.com>
From: Thomas A. Russ
Subject: Re: Newbie Q: How can I evaluate a form?
Date: 
Message-ID: <ymi8ymduqgu.fsf@sevak.isi.edu>
EVAL

(eval '(+ 2 2))  => 4
-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Henrik Motakef
Subject: Re: Newbie Q: How can I evaluate a form?
Date: 
Message-ID: <86d6bpe9io.fsf@pokey.internal.henrik-motakef.de>
······@mail.com (KanZen) writes:

> is there some Common Lisp function like: (evaluate-form some-form)
> or does no such thing exist?

(eval some-form)

But eval has a reputation of being misused by beginners more often
than not. If you are unsure, you might want to describe what you
actually want to do with it, there might be better solutions.
From: Peter Seibel
Subject: Re: Newbie Q: How can I evaluate a form?
Date: 
Message-ID: <m3y8ucduax.fsf@javamonkey.com>
······@mail.com (KanZen) writes:

> Henrik Motakef <············@henrik-motakef.de> wrote in message news:<··············@pokey.internal.henrik-motakef.de>...
> > ······@mail.com (KanZen) writes:
> > 
> > > is there some Common Lisp function like: (evaluate-form some-form)
> > > or does no such thing exist?
> > 
> > (eval some-form)
> > 
> > But eval has a reputation of being misused by beginners more often
> > than not. If you are unsure, you might want to describe what you
> > actually want to do with it, there might be better solutions.
> 
> I understand eval is frowned upon except when "you really need it", so I
> was trying to avoid it. funcall and apply nearly do what I want.
> 
> I'm not trying to do anything in particular; it's just that I've been
> looking at some examples and wondering: if I was defining a function and
> a form was passed in as an argument how would I evaluate it? This seems
> like an obvious sort of indirection to use every now and then.

So it turns out it's not that obvious a sort of indirection to use, in
a lot of cases. Especially since macros let you manipulate forms and
forms at compile time setting them up to be evaluated (or not)
according to the normal evaluation rules of the language.

Unless you really expect to be dealing with forms that can't possibly
be known at compile time (e.g. a user is going to type arbitrary forms
at you or you're going to read arbitrary forms off, say, a network
connection) you probably don't want to be using EVAL. On the other
hand if you *are* trying to intpret forms that come from the outside
world at runtime, EVAL may be just the thing. (Though you will want to
be careful about blindly EVAL'ing arbitrary forms given to you by an
untrusted source.)

-Peter

-- 
Peter Seibel                                      ·····@javamonkey.com

         Lisp is the red pill. -- John Fraser, comp.lang.lisp
From: Kalle Olavi Niemitalo
Subject: Re: Newbie Q: How can I evaluate a form?
Date: 
Message-ID: <874qwwqqvj.fsf@Astalo.kon.iki.fi>
To the OP:

Recall also that EVAL evaluates in a null lexical environment,
so that in

  (let ((x 1)
        (form '(+ x x)))
    (eval form))

the evaluated form will not see the local binding of X.  On the
other hand, a special variable will work:

  (defvar *x*)

  (let ((*x* 2)
        (form '(+ *x* *x*)))
    (eval form))

The following version also appears to work in CMUCL, but I don't
think it is really portable: although X is dynamically bound when
the form (+ X X) is evaluated, there is nothing that says the X
in that form should refer to the dynamic binding. [1]

  (let ((x 2)
        (form '(+ x x)))
    (declare (special x))
    (eval form))

This can be resolved by putting another SPECIAL declaration atop
the form:

  (let ((x 2)
        (form '(+ x x)))
    (declare (special x))
    (eval `(locally (declare (special x))
             ,form)))

However, if you choose to wrap the form inside other code, you
can as easily make the binding lexical:

  (let ((x 2)
        (form '(+ x x)))
    (eval `(let ((x ',x))
             ,form)))

Alternatively, you could convert the form into a function and
call it:

  (let* ((x 2)
         (form '(+ x x))
         (function (coerce `(lambda (x) ,form) 'function)))
    (funcall function x))

Finally, if you want to evaluate the form with multiple different
values of X, you can compile the function at runtime:

  (let* ((x-values '(1 2 3))
         (form '(+ x x))
         (function (compile nil `(lambda (x) ,form))))
    (mapcar function x-values))


Footnotes:
[1] I have done (setq extensions:*top-level-auto-declare* nil)
    but CMUCL doesn't give me any warning about this code.
From: Frode Vatvedt Fjeld
Subject: Re: Newbie Q: How can I evaluate a form?
Date: 
Message-ID: <2had6t1ggy.fsf@vserver.cs.uit.no>
······@mail.com (KanZen) writes:

> Suppose I have some function and in the body I want to evaluate a
> given form. It seems that I can do it with apply like this:
>
> (defun some-function (some-form)
>   (apply (first some-form) (rest some-form)))
>
> (some-function '(+ 2 2))
>
> This seems a little clumsy to me... is there some Common Lisp function like:
> (evaluate-form some-form) or does no such thing exist?

Evaluating a form is actually built into lisp, like this:

  (+ 2 2)

If on the other hand what you want to do is to apply to the function
that is the first element of a list some arguments that are the rest
of the elements of the list, then

  (apply (first list) (rest list))

seems to me not clumsy, but rather like a piece of
perfection. However, this is something else than the act of evaluating
a form, as in my former example, and as specified in CLHS chapter
3.1.2.1 "Form Evaluation". You will find that the apply example deals
only with a subset of all forms, namely those detailed in section
3.1.2.1.2.3 "Function Forms" (and furthermore restricted to accessing
the global environment). The eval function may be used to evaluate any
form in a null lexical environment, but often this is not what one
really wants to do.

-- 
Frode Vatvedt Fjeld