"logo.gif"  
Main
Read Online
Download PDF
Additional resources
Revision history
Known typos/bugs
Report a bug
License terms
About the author
"node191_1.gif" "node191_2.gif" "node191_3.gif"

4.7.2   Adding more definitions

Let us now add another definition to our function, so that it will give 2 on every even number :

"node191_4.gif"

Check now :

"node191_5.gif"

"node191_6.gif"

We see that nothing changed  - it does not work (It is interesting that the result is correct in version 6). The reason can be seen by looking at function definitions:

"node191_7.gif"

Global`f

f[x_Integer]:=1
f[x_Integer?EvenQ]:=2
f[x_]:=-1

By the way, the question mark in this context means the Information command, and returns the information contained in the global rule base on a given symbol (section 2.2.2).

We see that the reason for the above behavior is that Mathematica  was able to figure out that the pattern f[x_Integer?EvenQ] is more specific than the pattern f[x_], but unable to figure out that it is also more specific than f[x_Integer] (this refers to versions prior to 6.0. In 6.0, the pattern-matcher  does figure out the latter fact as well). The simplest thing one can do is to redefine the function, by placing definitions in a different order:

"node191_8.gif"

Check now :

"node191_9.gif"

"node191_10.gif"

"node191_11.gif" "node191_12.gif" "node191_13.gif"

Created by Wolfram Mathematica 6.0  (05 February 2009) Valid XHTML 1.1!