MathGroup Archive 1996

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

Search the Archive

Operator overloading with Symbol tags: Correction!


(I knew I should have eaten another Snickers bar before I posted the
previous message :)

I hand typed the previous example and mistakenly used a '+' instead of an '*'.  

This is the example using patterns:

In[99]:=
   (* -------BEGIN---------- *)
   $NewSymbol = Print["Name: ", #1, " Context: ", #2]&
   Fee; (* A Symbol 'Fee' *)
   Foo; (* A Symbol 'Foo' *)

   overloadMe[x:Fee] := x * 10 / Fee;
   overloadMe[x:Foo] := x * 100 / Foo;
   overloadMe[x_Symbol] := -1;

   overloadMe[ 10 Fee] + overloadMe[ 1 Foo]
   Name: Fee Context: Global`
   Name: Foo Context: Global`
   Name: overloadMe Context: Global`
Out[105]=
   100 + overloadMe[10 Fee]

This is a second example using delayed evaluation:
In[110]:=
   (* -------BEGIN---------- *)
   Fee; (* A Symbol 'Fee' *)
   Foo; (* A Symbol 'Foo' *)

   overloadMe[x_Fee] := x * 10 / Fee;
   overloadMe[x_Foo] := x * 100 / Foo;
   overloadMe[x_Symbol] := -1;

   overloadMe[ 10 Fee] + overloadMe[ 1 Foo]
Name: Fee Context: Global`
Name: Foo Context: Global`
Name: overloadMe Context: Global`
Out[116]=
   -1 + overloadMe[10 Fee]

Three questions:

A) Why isn't 'overloadedMe[10 Fee]' reduced to '-1' in both examples?

B) Relative to the second example, 
   If I unprotect 'Head' and specialize it so that 
  Head[ 1 Fee ] returns 'Fee' (and similarly for
  Foo), am I opening the Pandora's box of inexperience
  and leading my notebooks straight to _ell?

C) How do I overload the function arguments so that:
   In[58]:=
     overloadMe[ 10 Fee] + overloadMe[ 1 Foo] 
   Out[59]=
     200

Regards,
<cturner at bbn.com>

-- 
A. Chase Turner

-- 
A. Chase Turner

==== [MESSAGE SEPARATOR] ====


  • Prev by Date: image processing and Mma
  • Next by Date: Re: Wanted: a trick
  • Previous by thread: image processing and Mma
  • Next by thread: Re: Mathematica R^2 value