# Placement of pattern matching rules in functions [duplicate]

Is there any difference at all between

f[x_Integer /; x >= 0] := x + 1;
f[x_Integer] := x - 1;
f[x_] := Null;


and

f[x_Integer] := x + 1 /; x >= 0;
f[x_Integer] := x - 1;
f[x_] := Null;


The output is identical in both the cases. Even if there is no technical difference, is it common practice to adhere one and avoid the other?

• They are not the same. With the pattern on the right, the right side will evaluate anything up to the rule-constrained piece, while with the rule in the argument body, that won't happen. Try, e.g., changing the body of your example to have a prepended print: (Print["Hi"];...), then call each with -1 - the first will return unevaluated, the second evaluates the print. In your specific example, the end result is the same.
– ciao
Jun 1 '14 at 21:49
• @LeonidShifrin: Very informative link! +1
– ciao
Jun 1 '14 at 22:59
• @rasher I believe you are mistaken. The only time (that I recall) the body should be evaluating is when Condition is used inside With or Module. (As I mention here.) Jul 8 '14 at 8:19
• @Mr.Wizard: You are correct - I doofused and forgot to put parenthesis around compound statement...DOH!
– ciao
Jul 8 '14 at 8:28