"sum" quotes its arguments -- what's up with that?



Here's a toy sum function that doesn't use a with-respect-to  identifier

(%i2) xsum(f,l) := block([s : 0], for k in l do s : s + apply(f,[k]),s)$

(xsum is a toy --- it doesn't do everything sum does) 

(%i3) xsum(cos,[0,1,2]);
(%o3) cos(2)+cos(1)+1

(%i4) xsum(lambda([x],x^2),[1,2,3]);
(%o4) 14

(%i5) xsum(exp,[]);
(%o5) 0

I would be nice if 'apply'  knew the function algebra

(%i6) xsum(f+g,[0,1,2]);
f evaluates to g+f
Improper name or value in functional position.
#0: xsum(f=g+f,l=[0,1,2])
 -- an error.  Quitting.  To debug this try debugmode(true);

Just for fun ...(commercial macsyma did support a  '..' function)

(%i1) infix("..")$

(%i2) ".."(m,n) := block([s : []], 
   if integerp(m) and integerp(n) then 
      if (m < n) then for k : m thru n do s : endcons(k,s) else []
    else  s : funmake("..",[m,n]),s)$

(%i3) 0 .. 4;
(%o3)                           [0, 1, 2, 3, 4]
(%i4) (-1) .. 4;
(%o4)                        [- 1, 0, 1, 2, 3, 4]

Yeech, we need to be careful.... -1 .. 4 == -(1 .. 4)

(%i5) -1 .. 4;
(%o5)                        [- 1, - 2, - 3, - 4]

Barton