From: fix
Subject: Evaulate an arithmetic expression
Date: 
Message-ID: <cu61ep$dlh$1@news.tamu.edu>
Hi all,

I am writing a program to take derivative of a function and evaluate if 
given a value.

"deriv" is the function to do symbolic derivatives, the signiture is
defun deriv (expr var)
and the function take derivative of expr with respect to var.
so if I do
(deriv '(* x x) 'x), it returns (+ x x). (Same as 2x, but it doesn't 
matter.)

And I want (deriv-eval '(* x x) 'x 5) to return 10.

This is what I have written for deriv-eval:
(defun deriv-eval (expr var val)
	(setf derivans (deriv expr var))
	(setf var val)
	(eval derivans)
)

But it doesn't work for the last line, here is the error:
Error in KERNEL::UNBOUND-SYMBOL-ERROR-HANDLER:  the variable X is unbound.
    [Condition of type UNBOUND-VARIABLE]

I was thinking of, in the first line, get the answer, then give the 
variable a value, and evaulate. Am I wrong?

Thanks.
fix.

From: David Sletten
Subject: Re: Evaulate an arithmetic expression
Date: 
Message-ID: <FzwNd.8587$WD4.4827@twister.socal.rr.com>
fix wrote:

> Hi all,
> 
> I am writing a program to take derivative of a function and evaluate if 
> given a value.
> 
> "deriv" is the function to do symbolic derivatives, the signiture is
> defun deriv (expr var)
> and the function take derivative of expr with respect to var.
> so if I do
> (deriv '(* x x) 'x), it returns (+ x x). (Same as 2x, but it doesn't 
> matter.)
> 
> And I want (deriv-eval '(* x x) 'x 5) to return 10.
> 
> This is what I have written for deriv-eval:
> (defun deriv-eval (expr var val)
>     (setf derivans (deriv expr var))
>     (setf var val)
>     (eval derivans)
> )
> 
> But it doesn't work for the last line, here is the error:
> Error in KERNEL::UNBOUND-SYMBOL-ERROR-HANDLER:  the variable X is unbound.
>    [Condition of type UNBOUND-VARIABLE]
> 
> I was thinking of, in the first line, get the answer, then give the 
> variable a value, and evaulate. Am I wrong?
> 
> Thanks.
> fix.
> 

You misunderstand the macro SETF. However, you should be able to see 
that something is wrong just by looking closely at your code. First, you 
use SETF to assign a value to a variable named DERIVANS. In other words, 
you expect SETF to directly use the symbol you provide it to name a 
variable. In the second case you are trying to use SETF to assign a 
value not to the variable named VAR but rather to the symbol that VAR 
refers to. Clearly this is an inconsistency--SETF can't possibly know 
that you intend indirection in one case but not the other.

SETF does not evaluate its first argument, which identifies the 'place' 
where it is to store the value of the second argument. Here, to indicate 
that the place is the global variable named by VAR's value, you should 
use SYMBOL-VALUE. Furthermore, it is better to create a local variable 
DERIVANS using LET rather than relying on another global variable:
(defun deriv-eval (expr var val)
   (let ((derivans (deriv expr var)))
     (setf (symbol-value var) val)
     (eval derivans)))

David Sletten
From: fix
Subject: Re: Evaulate an arithmetic expression
Date: 
Message-ID: <cu6br2$lpa$1@news.tamu.edu>
David Sletten wrote:
> fix wrote:
> 
>> Hi all,
>>
>> I am writing a program to take derivative of a function and evaluate 
>> if given a value.
>>
>> "deriv" is the function to do symbolic derivatives, the signiture is
>> defun deriv (expr var)
>> and the function take derivative of expr with respect to var.
>> so if I do
>> (deriv '(* x x) 'x), it returns (+ x x). (Same as 2x, but it doesn't 
>> matter.)
>>
>> And I want (deriv-eval '(* x x) 'x 5) to return 10.
>>
>> This is what I have written for deriv-eval:
>> (defun deriv-eval (expr var val)
>>     (setf derivans (deriv expr var))
>>     (setf var val)
>>     (eval derivans)
>> )
>>
>> But it doesn't work for the last line, here is the error:
>> Error in KERNEL::UNBOUND-SYMBOL-ERROR-HANDLER:  the variable X is 
>> unbound.
>>    [Condition of type UNBOUND-VARIABLE]
>>
>> I was thinking of, in the first line, get the answer, then give the 
>> variable a value, and evaulate. Am I wrong?
>>
>> Thanks.
>> fix.
>>
> 
> You misunderstand the macro SETF. However, you should be able to see 
> that something is wrong just by looking closely at your code. First, you 
> use SETF to assign a value to a variable named DERIVANS. In other words, 
> you expect SETF to directly use the symbol you provide it to name a 
> variable. In the second case you are trying to use SETF to assign a 
> value not to the variable named VAR but rather to the symbol that VAR 
> refers to. Clearly this is an inconsistency--SETF can't possibly know 
> that you intend indirection in one case but not the other.
> 
> SETF does not evaluate its first argument, which identifies the 'place' 
> where it is to store the value of the second argument. Here, to indicate 
> that the place is the global variable named by VAR's value, you should 
> use SYMBOL-VALUE. Furthermore, it is better to create a local variable 
> DERIVANS using LET rather than relying on another global variable:
> (defun deriv-eval (expr var val)
>   (let ((derivans (deriv expr var)))
>     (setf (symbol-value var) val)
>     (eval derivans)))
> 
> David Sletten

I got it. Thanks so much. But is there any website that has all the 
built-in function of LISP? I am actually using LISP in an AI course, but 
neither the prof nor the textbook talk much about LISP. Or should I go 
get another book? Thanks!

fix.
From: David Sletten
Subject: Re: Evaulate an arithmetic expression
Date: 
Message-ID: <dOCNd.8631$WD4.1575@twister.socal.rr.com>
fix wrote:


>> (defun deriv-eval (expr var val)
>>   (let ((derivans (deriv expr var)))
>>     (setf (symbol-value var) val)
>>     (eval derivans)))
>>

This could be even simpler:
(defun deriv-eval (expr var val)
   (setf (symbol-value var) val)
   (eval (deriv expr val)))

> I got it. Thanks so much. But is there any website that has all the 
> built-in function of LISP? I am actually using LISP in an AI course, but 
> neither the prof nor the textbook talk much about LISP. Or should I go 
> get another book? Thanks!
> 

Your one-stop reference is the Common Lisp HyperSpec:
http://www.lispworks.com/documentation/HyperSpec/Front/index.htm

If you would like to read a helpful text:
http://www.gigamonkeys.com/book/

David Sletten
From: ······@gmail.com
Subject: Re: Evaulate an arithmetic expression
Date: 
Message-ID: <1107754073.295277.315260@f14g2000cwb.googlegroups.com>
David Sletten wrote:
> fix wrote:
>
>
> >> (defun deriv-eval (expr var val)
> >>   (let ((derivans (deriv expr var)))
> >>     (setf (symbol-value var) val)
> >>     (eval derivans)))
> >>
>
> This could be even simpler:
> (defun deriv-eval (expr var val)
>    (setf (symbol-value var) val)
>    (eval (deriv expr val)))

Or even:

(defun deriv-eval (expr var val)
  (set var val)
  (eval (deriv expr var)))

However, I don't know what the community consensus is on #'set.  Is
it's usage frowned upon?  I never seem to see it used.  Same with
#'setq.  I've heard people say that you should always just use #'setf.

Justin Dubs
From: David Sletten
Subject: Re: Evaulate an arithmetic expression
Date: 
Message-ID: <c8ENd.7622$e11.5462@twister.socal.rr.com>
······@gmail.com wrote:


> 
> (defun deriv-eval (expr var val)
>   (set var val)
>   (eval (deriv expr var)))
> 
> However, I don't know what the community consensus is on #'set.  Is
> it's usage frowned upon?  I never seem to see it used.  Same with
> #'setq.  I've heard people say that you should always just use #'setf.
> 
> Justin Dubs
> 

According to CLHS, SET is deprecated:
http://www.lispworks.com/documentation/HyperSpec/Body/f_set.htm

You'll have to ask one of the gurus about why.

David Sletten
From: Pascal Costanza
Subject: Re: Evaulate an arithmetic expression
Date: 
Message-ID: <cu7e0v$mml$1@snic.vub.ac.be>
David Sletten wrote:

> ······@gmail.com wrote:
>>
>> (defun deriv-eval (expr var val)
>>   (set var val)
>>   (eval (deriv expr var)))
>>
>> However, I don't know what the community consensus is on #'set.  Is
>> it's usage frowned upon?  I never seem to see it used.  Same with
>> #'setq.  I've heard people say that you should always just use #'setf.
>>
>> Justin Dubs
>>
> 
> According to CLHS, SET is deprecated:
> http://www.lispworks.com/documentation/HyperSpec/Body/f_set.htm
> 
> You'll have to ask one of the gurus about why.

SET evaluates its first argument, SETQ doesn't (the Q stands for 
"quoted"). The first argument to SET must evaluate to a symbol whose 
global symbol-value will be changed. The first argument to SETQ must be 
a symbol that refers to a possibly local variable. It's not possible to 
make SET refer to local variables.

The first Lisps that were dynamically scoped didn't make a distinction 
between (SETQ var value) and (SET (QUOTE var) value), because there is 
none. In a lexically scoped Lisp (like Common Lisp) these are two very 
different operations. SET was deprecated in order to avoid confusion and 
be more explicit what is actually meant.


Pascal
From: ······@gmail.com
Subject: Re: Evaulate an arithmetic expression
Date: 
Message-ID: <1107788342.166921.303820@l41g2000cwc.googlegroups.com>
Pascal Costanza wrote:
> David Sletten wrote:
>
> > ······@gmail.com wrote:
> >>
> >> (defun deriv-eval (expr var val)
> >>   (set var val)
> >>   (eval (deriv expr var)))
> >>
> >> However, I don't know what the community consensus is on #'set.
Is
> >> it's usage frowned upon?  I never seem to see it used.  Same with
> >> #'setq.  I've heard people say that you should always just use
#'setf.
> >>
> >> Justin Dubs
> >>
> >
> > According to CLHS, SET is deprecated:
> > http://www.lispworks.com/documentation/HyperSpec/Body/f_set.htm
> >
> > You'll have to ask one of the gurus about why.
>
> SET evaluates its first argument, SETQ doesn't (the Q stands for
> "quoted"). The first argument to SET must evaluate to a symbol whose
> global symbol-value will be changed. The first argument to SETQ must
be
> a symbol that refers to a possibly local variable. It's not possible
to
> make SET refer to local variables.

Hey, thanks.  Great point.  I had just explained a similar thing with
respect to #'funcall in another thread, but for some reason this didn't
occur to me.  :-(

> The first Lisps that were dynamically scoped didn't make a
distinction
> between (SETQ var value) and (SET (QUOTE var) value), because there
is
> none. In a lexically scoped Lisp (like Common Lisp) these are two
very
> different operations. SET was deprecated in order to avoid confusion
and
> be more explicit what is actually meant.

Gotcha.  Thanks again.

Justin Dubs
From: Kalle Olavi Niemitalo
Subject: SET, SETQ, and SETF (was: Evaulate an arithmetic expression)
Date: 
Message-ID: <876514eurm.fsf_-_@Astalo.kon.iki.fi>
······@gmail.com writes:

> However, I don't know what the community consensus is on #'set.  Is
> it's usage frowned upon?  I never seem to see it used.

It is deprecated, and the replacement SETF SYMBOL-VALUE is harder
to misunderstand, I think.

> Same with #'setq.  I've heard people say that you should always
> just use #'setf.

There is no (function setq) nor (function setf).

Out of habit, I use SETQ for variables and SETF for other
places.  If I know the place is actually a symbol macro, then I
use SETF, even though SETQ would work as well.  I don't think
there is anything wrong with teaching people to just use SETF,
provided they can understand SETQ when they see it.
From: fix
Subject: Re: Evaulate an arithmetic expression
Date: 
Message-ID: <cu7kba$mk5$1@news.tamu.edu>
David Sletten wrote:

> fix wrote:
> 
> 
>>> (defun deriv-eval (expr var val)
>>>   (let ((derivans (deriv expr var)))
>>>     (setf (symbol-value var) val)
>>>     (eval derivans)))
>>>
> 
> This could be even simpler:
> (defun deriv-eval (expr var val)
>   (setf (symbol-value var) val)
>   (eval (deriv expr val)))
> 

But I am not too sure how it works.
The setf is giving a value to the variable,
and the second line is differentiating expr with respect to a number?

... [snip] ...
From: David Sletten
Subject: Re: Evaulate an arithmetic expression
Date: 
Message-ID: <PINNd.8848$WD4.345@twister.socal.rr.com>
fix wrote:

> 
> David Sletten wrote:
> 
>> fix wrote:
>>
>>
>>>> (defun deriv-eval (expr var val)
>>>>   (let ((derivans (deriv expr var)))
>>>>     (setf (symbol-value var) val)
>>>>     (eval derivans)))
>>>>
>>
>> This could be even simpler:
>> (defun deriv-eval (expr var val)
>>   (setf (symbol-value var) val)
>>   (eval (deriv expr val)))
>>
> 
> But I am not too sure how it works.
> The setf is giving a value to the variable,
> and the second line is differentiating expr with respect to a number?
> 
> ... [snip] ...
> 

In the more verbose version we store the value of the derivation in a 
temporary variable, DERIVANS. This is the derivative expression, e.g., 
(* x x) -> (* 2 x) [or (+ x x) in your example]. We then assign a value 
to X and ask Lisp to EVAL the derivative.

In the terser example, we simply use the derivative directly rather than 
storing it temporarily. The fact that we've already assigned a value to 
X is irrelevant. Your derivative code isn't evaluating variables, is it? 
It's simply looking for the symbol with respect to which you are 
differentiating right? In other words, differentiating 3x^2 - 4x wrt x 
is 6x - 4 regardless of the value of x. Even if you know ahead of time 
that x = 3 you can't evaluate 3(3^2) - 4(3) and then take the derivative.

In any case, take a look at Alan's example. He has a better approach.

David Sletten
From: fix
Subject: Re: Evaulate an arithmetic expression
Date: 
Message-ID: <cuc5ik$pku$1@news.tamu.edu>
David Sletten wrote:
> fix wrote:
> 
>>
>> David Sletten wrote:
>>
>>> fix wrote:
>>>
>>>
>>>>> (defun deriv-eval (expr var val)
>>>>>   (let ((derivans (deriv expr var)))
>>>>>     (setf (symbol-value var) val)
>>>>>     (eval derivans)))
>>>>>
>>>
>>> This could be even simpler:
>>> (defun deriv-eval (expr var val)
>>>   (setf (symbol-value var) val)
>>>   (eval (deriv expr val)))
>>>
>>
>> But I am not too sure how it works.
>> The setf is giving a value to the variable,
>> and the second line is differentiating expr with respect to a number?
>>
>> ... [snip] ...
>>
> 
> In the more verbose version we store the value of the derivation in a 
> temporary variable, DERIVANS. This is the derivative expression, e.g., 
> (* x x) -> (* 2 x) [or (+ x x) in your example]. We then assign a value 
> to X and ask Lisp to EVAL the derivative.

Yes this one make sense to me.

> In the terser example, we simply use the derivative directly rather than 
> storing it temporarily. The fact that we've already assigned a value to 
> X is irrelevant. Your derivative code isn't evaluating variables, is it? 
> It's simply looking for the symbol with respect to which you are 
> differentiating right? In other words, differentiating 3x^2 - 4x wrt x 
> is 6x - 4 regardless of the value of x. Even if you know ahead of time 
> that x = 3 you can't evaluate 3(3^2) - 4(3) and then take the derivative.

Do you mean "3" will be the symbol if you set x=3?
It is just messing up my mind, I can't decide when it is a variable and 
when it is a number.
What if I deriv (* 3 x) and substitute x=3? The "3" in (* 3 x) would not 
be regarded as a variable and be differentiated?

> In any case, take a look at Alan's example. He has a better approach.

Alan's?

> David Sletten
From: David Sletten
Subject: Re: Evaulate an arithmetic expression
Date: 
Message-ID: <SCiOd.14171$BS.10067@twister.socal.rr.com>
fix wrote:


>>>> This could be even simpler:
>>>> (defun deriv-eval (expr var val)
>>>>   (setf (symbol-value var) val)
>>>>   (eval (deriv expr val)))
>>>>

>> In the terser example, we simply use the derivative directly rather 
>> than storing it temporarily. The fact that we've already assigned a 
>> value to X is irrelevant. Your derivative code isn't evaluating 
>> variables, is it? It's simply looking for the symbol with respect to 
>> which you are differentiating right? In other words, differentiating 
>> 3x^2 - 4x wrt x is 6x - 4 regardless of the value of x. Even if you 
>> know ahead of time that x = 3 you can't evaluate 3(3^2) - 4(3) and 
>> then take the derivative.
> 
> 
> Do you mean "3" will be the symbol if you set x=3?
> It is just messing up my mind, I can't decide when it is a variable and 
> when it is a number.
> What if I deriv (* 3 x) and substitute x=3? The "3" in (* 3 x) would not 
> be regarded as a variable and be differentiated?
>

The process of differentiation of polynomials is simply a formal 
transformation. Regardless of whether the variable is x or y or z, we 
take the pattern <var>^2 and produce 2*<var>. This is what your code is 
doing in DERIV. It takes the expression (* x x) and produces (+ x x). It 
would transform the expression (* z z) into (+ z z), right? This process 
does not involve evaluating variables--it simply transforms one 
expression into another. It's only when we ask Lisp to EVAL the 
derivative expression that we need concern ourselves with what X (or Z) 
represents. So in the shorter version above, the assignment of a value 
to X is independent of the production of the derivative expression. As 
long as we do the SETF _sometime_ before we call EVAL we're OK. Because 
EVAL sees the derivative expression, (+ x x), for instance and has to 
determine "what is the value of X?".


>> In any case, take a look at Alan's example. He has a better approach.
> 
> 
> Alan's?
Take a look at the example Alan Crowe presented elsewhere in this 
thread. It uses PROGV to limit the scope of the variables you evaluate 
in your derivatives.

David Sletten
From: ···············@yahoo.com
Subject: Re: Evaulate an arithmetic expression
Date: 
Message-ID: <1107790599.156630.143570@c13g2000cwb.googlegroups.com>
For a student in an AI course, one textbook I'd recommend is _Lisp_,
3rd. ed. (or 2nd), by Winston and Horn.  It was written partly for
students whose next course was going to be AI.

As David says, the Hyperspec is the online reference for "all the
built-in functions".
From: Kalle Olavi Niemitalo
Subject: Re: Evaulate an arithmetic expression
Date: 
Message-ID: <878y61e1tx.fsf@Astalo.kon.iki.fi>
fix <···@tamu.edu> writes:

> And I want (deriv-eval '(* x x) 'x 5) to return 10.

I think you mean 25.  Here are a few ways to do it.

  (defun deriv-eval (form variable value)
    (eval `(let ((,variable ',value)) ,form)))

  (defun deriv-eval (form variable value)
    (funcall (coerce `(lambda (,variable) ,form) 'function)
             value))

If you need to evaluate the expression with multiple different
values for the variable (e.g. for a graph), you can modify the
latter definition to generate the function just once, hopefully
speeding up the process.  You could also COMPILE the function at
runtime, although that may actually be slower if you then call it
just a few times.

> (defun deriv-eval (expr var val)
> 	(setf derivans (deriv expr var))
> 	(setf var val)

Suppose the value of VAR is X, and the value of VAL is 5.  Then,
this (setf var val) will just set VAR to 5, rather than modify X
in any way.  (setf (symbol-value var) val) would be closer to
what you need, but it has two problems:

1. The changed value persists even after the function returns, so
   it can cause errors if some other part of the program is using
   the variable X for another purpose.

2. I think, in principle, the form evaluated with EVAL should
   explicitly declare any special variables it uses.  However,
   the dictionary entry for EVAL says SYMBOL-VALUE is equivalent
   to it, so I'm not sure the declarations are required in this
   context.

You could fix the first problem with (progv `(,var) `(,val) ...) and
the second with (eval `(locally (declare (special ,var)) ,expr)),
but that's so much typing that you could as well use one of the
lexical-variable solutions I showed above.
From: Alan Crowe
Subject: Re: Evaulate an arithmetic expression
Date: 
Message-ID: <86oeewe5kw.fsf@cawtech.freeserve.co.uk>
···@tamu.edu quested:
> And I want (deriv-eval '(* x x) 'x 5) to return 10.

CL-USER(116): (defun deriv-eval (expr var val)
		(progv (list var) (list val)
		  (eval (deriv expr var))))
DERIV-EVAL
CL-USER(117): (deriv-eval '(* x x) 'x 5)
10

Although the names are unrelated, PROGV is a variant of LET.
You could come to this code in two steps.
First we cheat with

CL-USER(118): (defun cheating-deriv-eval (expr var val)
		(declare (ignore var))
		(let ((x val))
		  (declare (special x))
		  (eval (deriv expr var))))

Which gives the right answer, but only because we are
ignoring VAR and hard-coding X as the name of our variable.

The issue is that this:

CL-USER(113): (let ((x (quote a))
		    (y (quote b)))
		(let ((x 5) ;supposed to bind a to 5
		      (y 7));supposed to bind b to 7
		  (+ a b)))
Error: Attempt to take the value of the unbound variable `A'.
  [condition type: UNBOUND-VARIABLE]

doesn't work, because first LET helps us by not evaluating
the variable names, x and y, and then LET thwarts us by not
evaluating the variable names, x and y.

We need to use PROGV on the inner binding

CL-USER(114): (let ((x (quote a))
		    (y (quote b)))
		(progv (list x y)
		       (list 5 7)
		  (+ a b)))
12 
 
Success!

Why does nobody know about PROGV? Well, take a look at the
body form of my example (+ A B). If we know that the
varibles are going to be called A and B we can type them
directly into a LET, and not bother with PROGV at all.
PROGV only makes sense when the variable names are not known
until runtime. Hence the comment at the bottom of 

http://www.franz.com/support/documentation/6.2/ansicl/dictentr/progv.htm

     Among other things, progv is useful when writing
     interpreters for languages embedded in Lisp; it
     provides a handle on the mechanism for binding dynamic
     variables.

Alan Crowe
Edinburgh
Scotland
From: David Sletten
Subject: Re: Evaulate an arithmetic expression
Date: 
Message-ID: <syNNd.8842$WD4.4001@twister.socal.rr.com>
Alan Crowe wrote:

> ···@tamu.edu quested:
> 
>>And I want (deriv-eval '(* x x) 'x 5) to return 10.
> 
> 
> CL-USER(116): (defun deriv-eval (expr var val)
> 		(progv (list var) (list val)
> 		  (eval (deriv expr var))))
> DERIV-EVAL
> CL-USER(117): (deriv-eval '(* x x) 'x 5)
> 10
> 
> Although the names are unrelated, PROGV is a variant of LET.
> You could come to this code in two steps.
> First we cheat with
> 
> CL-USER(118): (defun cheating-deriv-eval (expr var val)
> 		(declare (ignore var))
> 		(let ((x val))
> 		  (declare (special x))
> 		  (eval (deriv expr var))))
> 
> Which gives the right answer, but only because we are
> ignoring VAR and hard-coding X as the name of our variable.
> 
> The issue is that this:
> 
> CL-USER(113): (let ((x (quote a))
> 		    (y (quote b)))
> 		(let ((x 5) ;supposed to bind a to 5
> 		      (y 7));supposed to bind b to 7
> 		  (+ a b)))
> Error: Attempt to take the value of the unbound variable `A'.
>   [condition type: UNBOUND-VARIABLE]
> 
> doesn't work, because first LET helps us by not evaluating
> the variable names, x and y, and then LET thwarts us by not
> evaluating the variable names, x and y.
> 
> We need to use PROGV on the inner binding
> 
> CL-USER(114): (let ((x (quote a))
> 		    (y (quote b)))
> 		(progv (list x y)
> 		       (list 5 7)
> 		  (+ a b)))
> 12 
>  
> Success!
> 
> Why does nobody know about PROGV? Well, take a look at the
> body form of my example (+ A B). If we know that the
> varibles are going to be called A and B we can type them
> directly into a LET, and not bother with PROGV at all.
> PROGV only makes sense when the variable names are not known
> until runtime. Hence the comment at the bottom of 
> 
> http://www.franz.com/support/documentation/6.2/ansicl/dictentr/progv.htm
> 
>      Among other things, progv is useful when writing
>      interpreters for languages embedded in Lisp; it
>      provides a handle on the mechanism for binding dynamic
>      variables.
> 
> Alan Crowe
> Edinburgh
> Scotland
> 

Nice, Alan. That eliminates the need to muck around with a global X. 
Thanks for the explanation.

David Sletten