1
$\begingroup$

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?

$\endgroup$
4
  • 2
    $\begingroup$ 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. $\endgroup$
    – ciao
    Jun 1 '14 at 21:49
  • $\begingroup$ @LeonidShifrin: Very informative link! +1 $\endgroup$
    – ciao
    Jun 1 '14 at 22:59
  • $\begingroup$ @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.) $\endgroup$
    – Mr.Wizard
    Jul 8 '14 at 8:19
  • $\begingroup$ @Mr.Wizard: You are correct - I doofused and forgot to put parenthesis around compound statement...DOH! $\endgroup$
    – ciao
    Jul 8 '14 at 8:28