I'm a big fan of using conditionals inside my functions to deal with cases, that is, to pattern match in stages and possibly leave a function unevaluated.
In[1]:= f[x_] :=
(
(
(
True
) /; EvenQ[x]
) /; x > 2
) /; x < 10
In[2]:= f[4]
Out[2]= True
However I don't fully understand the extent to which I can do this. For example, mixing If statements in doesn't work:
In[3]:=
h[x_] :=
(
If[x > 2,
(
True
) /; EvenQ[x]
,
False
]
) /; x < 10
In[4]:= h[4]
Out[4]= True /; EvenQ[4]
A clarification on both the limitation and merits of this style of coding would be helpful.
I find this style of programming to be handy for a number of reasons, but a primary reason is the ease of defining a blanket default case and letting the pattern matching do its work, e.g. f[_] := $Failed.
If-s etc), you are likely to generate such situations. Have a look here for a relevant discussion. – Leonid Shifrin Jun 15 '12 at 08:11f[_] := $Failedbehavior usingAnd; also, you still have not described the output you desire fromh. – Mr.Wizard Jun 16 '12 at 00:02