Re: The uses of Condition (/;)
- To: mathgroup at smc.vnet.net
- Subject: [mg82956] Re: [mg82926] The uses of Condition (/;)
- From: "Szabolcs HorvÃt" <szhorvat at gmail.com>
- Date: Mon, 5 Nov 2007 04:56:24 -0500 (EST)
- References: <200711030831.DAA19563@smc.vnet.net>
On 11/3/07, Ingolf Dahl <ingolf.dahl at telia.com> wrote: > I think the real example in case C should be > > h1[x_] := Module[{}, (Print["def"]; x) /; (Print["cond"]; x > 5)] > > because your h[x_] expression is interpreted as > > h[x_] := Module[{}, Print["def"]; (x /; (Print["cond"]; x > 5))] > > which does not correspond to the case C. It instead corresponds to > > D. pattern := Module[{}, expr1; (definition /; condition)] > > This leads us to an answer to your question. We define a new pattern, a > modification of pattern B: > > E. pattern := (expr1; (definition /; condition)) > > with the example > > g1[x_] := (Print["expr1"]; ((Print["def"]; x) /; (Print["cond"]; x > 5))) > > In[8]:= g1[0] > > During evaluation of In[8]:= expr1 > > During evaluation of In[8]:= cond > > Out[8]= g1[0] > > This function behaves similar to the D function, and we see that Module is > just a wrapper. Module here is useful if we want to share local variables > between the definition and the expression, so that we can transfer sub > results and do not have to repeat calculations. > I do not see how to modify A to get the same kind of evaluation as with the > case E. > See also the MathGroup discussion about precedence of operators earlier this > week. > Thanks for the reply (and for pointing out that /; has a higher precedence than ;)! So in fact cases B and C are sub-cases of the same usage, and Module has no importance. All that matters is that Condition[] be the expression that would be returned by the function. (Well, almost: Condition[] must be returned by a restricted set of built-ins, like CompoundExpression or Module. f[x_] := Identity[x /; x > 5] does not work. But this is understandable, since in Mathematica the "return value" is not a well defined concept.) Szabolcs
- References:
- The uses of Condition (/;)
- From: Szabolcs Horvát <szhorvat@gmail.com>
- The uses of Condition (/;)