# [Maxima] Newbie question about quotation

Stavros Macrakis macrakis at alum.mit.edu
Wed Dec 27 12:08:16 CST 2006

```On 12/27/06, Michel Van den Bergh <michel.vandenbergh at uhasselt.be> wrote:
> Since levin_u_sum should behave like sum I think it is best to define it as a macro....
> For the arguments that should be evaluated I perform immediately
> arg:ev(arg,nouns). Is
> that the correct strategy?

If you look at the code for sum (asum.lisp, function dosum), you will
see that it is messier than that, and it will be hard to reproduce the
behavior in user code.  Moreover, it will likely lead to all sorts of
bizarre behavior and bugs.  This is an area we've debated before among
the Maxima developers and frankly I don't think the current behavior
(though it is an improvement on previous behavior) is the last word.

Here are some fun cases which show the current behavior and how it
interacts with Maxima's handling of conditionals, mathematical vs.
programming functions, quoted expressions, and noun forms:

sum(print(i),i,1,n);  (interesting -- shows you the internal workings)

sum(i,i,1,3) => 6  (OK)
j: 'i\$
sum(j,i,1,3) => 6 (OK, I guess) *

sum(max(i,2),i,1,3) => 7 (OK)
sum(max(i,2),i,1,n) =>  'sum(max(i,2),i,1,n)  (OK)

sum(gcd(i,i+3),i,1,10) => 16 (OK)
sum(gcd(i,i+3),i,1,n) => n  (oops, gcd is a programming function, not
a math function like max)

sum( if oddp(i) then i else 0, i, 1, 5)     => 9   (consistent with (*) )
sum( if oddp(j) then j else 0, i, 1, 5)     => 0   (not consistent with (*) )
sum( '(if oddp(j) then j else 0), i, 1, 5)  => 5 * (if oddp(j) then j
else 0)  (!!)

ff(i):=if oddp(i) then i else 0\$
sum(ff(i),i,1,5) => 9 (consistent)
sum(ff(j),i,1,5) => 0 (not consistent with (*) )

sum(diff(log(x),x,i),i,1,3) =>  1/x-1/x^2+2/x^3 (OK)
sum(diff(log(x),x,j),i,1,3) =>
'diff(log(x),x,3)+'diff(log(x),x,2)+'diff(log(x),x,1) (??!!)

form: diff(log(x),x,i)\$
sum(foo,i,1,3) =>
'diff(log(x),x,3)+'diff(log(x),x,2)+'diff(log(x),x,1)  (so much for
the auto substitution)

/* OK, let's try to avoid the noun-form problem */
foo: '(diff(log(x),x,i));
sum(foo,i,1,3) => diff(log(x),x,3)+diff(log(x),x,2)+diff(log(x),x,1)  (hmm)

Hope you find this interesting,

-s
```