MathGroup Archive 1997

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Overloading mathematica built in functions

  • To: mathgroup at
  • Subject: [mg5749] Re: [mg5716] Overloading mathematica built in functions
  • From: Allan Hayes <hay at>
  • Date: Tue, 14 Jan 1997 10:42:28 -0500
  • Sender: owner-wri-mathgroup at

seanross at
[mg5716] Overloading mathematica built in functions

>I want to add a capability to the Sum function.  If I give it a
>single argument that is a 1-D list, I want it to sum the list.
>does not work.  Does anyone know why?  I am guessing it has  
>something to do with the attribute HoldAll on the built-in function  

Yes, it is because of the HoldAll attribute: if
w = {1,2,3};
then in Sum[w], w is not evaluated to a list and the definition is  
not used. But, with


The test is evaluated (in spite of the HoldAll) and we get

However, the following is much more faster
Sum[f_/;Head[f]===List]:= Plus@@f;
And even here, f is evaluated twice, so the following variant may  
be better

Sum[f_]:= Module[{g},Plus@@g/;Head[g = f]===List];

Allan Hayes,
hay at


  • Prev by Date: Printing to HP LaserJet4
  • Next by Date: Memory saving mode of mathematica front end?
  • Previous by thread: Re: Overloading mathematica built in functions
  • Next by thread: Mathematica on Daystar