MathGroup Archive 2007

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

Search the Archive

Re: ClearAll[f]; f[x_] := x^2; f[y_] :=y^4; (*What is:*) f[2]

  • To: mathgroup at
  • Subject: [mg84397] Re: ClearAll[f]; f[x_] := x^2; f[y_] :=y^4; (*What is:*) f[2]
  • From: Albert Retey <awnl at>
  • Date: Fri, 21 Dec 2007 03:22:25 -0500 (EST)
  • References: <fkcueh$5d9$>


> ClearAll[f]; f[x_] := x^2; f[y_] :=y^4; (*What is:*) f[2]
> Evaluating this line in Mathematica 5.2 or Mathematica 6 returns 16. This makes sense, because the second definition replaces the first, as we can see when ?f returns:
> Global`f
> f[y_]:=y^4
> But in _A_Physicist's_Guide_to_Mathematica_ on p.314, Patrick Tam shows an example like this returning the other answer, 4, defined in the first definition. He then demonstrates that ?f returns:
> Global`f
> f[x_] := x^2
> f[y_]:= y^4
> He says his book was developed with Mathematica 2.2 and a prerelease of Mathematica 3 and is compatible with both.
> He goes on to explain:
> "Contrary to expectation, Mathematica used the first definition. The ? operator reveals that Mathematica stores both definitions in the global rule base, giving higher priority to the first definition. (This problem cannot, perhaps, be called a bug because developers of Mathematica are well aware of this design flaw, which is quite difficult to mend....)"
> What is he talking about? Did Mathematica  2.2 and 3 treat this differently? If earlier versions worked in this surprising way, there must have been a reason - what was it? Was it changed to prevent surprises like this example? Did changing it create other unfortunate consequences? Was Tam just wrong? Or do I misunderstand?

I think the statement is still true. Usually mathematica orders the 
definitions so that more specific definitions are tested before more 
general ones. When there is a real ambiguity concerning which pattern is 
more specific the definitions are tested in the order they were entered. 
For this very simple case (where just the name of the pattern is 
different) it is not really an ambiguity and thus obviously can and has 
been overcome in newer versions. Look at the following for an example 
which shows that the behavior still exists for ambiguities which can not 
be resolved as easy as the above:


Note that for an integer both definitions match and the one which is 
entered first will win which you can check by changing the order of the 
two definitions. While in this case in principle it could be argued that 
the IntegerQ test is more specific than the NumericQ test, this would be 
much harder to detect and there is no way for mathematica to decide 
which of two such definitions is more specific if you would replace 
IntegerQ and NumericQ with two user defined functions.



  • Prev by Date: Re: checking, if a file exists
  • Next by Date: Re: FindRoot / Jacobian
  • Previous by thread: Re: ClearAll[f]; f[x_] := x^2; f[y_] :=y^4; (*What is:*) f[2]
  • Next by thread: checking, if a file exists