MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: The uses of Condition (/;). second thoght

  • To: mathgroup at smc.vnet.net
  • Subject: [mg82938] RE: [mg82926] The uses of Condition (/;). second thoght
  • From: "Ingolf Dahl" <ingolf.dahl at telia.com>
  • Date: Sun, 4 Nov 2007 06:07:03 -0500 (EST)
  • References: <200711030831.DAA19563@smc.vnet.net>

A second thought, after pressing the send button too fast: it is of course
possible to modify case A to simulate my g1 function below. It is just to
write

g2[x_] /; (Print["expr1"]; Print["cond"]; x > 5) := (Print["def"]; x)

But anyway it is logical to have case B if we also should have case C, which
enables sharing of local variables. There is no other evaluation difference
between these cases.

Best regards

Ingolf Dahl

> -----Original Message-----
> From: Ingolf Dahl [mailto:ingolf.dahl at telia.com]
> Sent: den 3 november 2007 21:26
> To: 'Szabolcs Horv=E1t'; 'mathgroup at smc.vnet.net'
> Subject: RE: [mg82926] The uses of Condition (/;)
>
> 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.
>
> Best regards
>
> Ingolf Dahl
>
> > -----Original Message-----
> > From: Szabolcs Horv=E1t [mailto:szhorvat at gmail.com]
> > Sent: den 3 november 2007 09:32
> > To: mathgroup at smc.vnet.net
> > Subject: [mg82926] The uses of Condition (/;)
> >
> >
> > According to the documentation, /; can be used in three different
> > ways, illustrated below:
> >
> > A. pattern /; condition = definition
> >
> > B. pattern := definition /; condition
> >
> > C. pattern := Module[{}, definition /; condition]
> >
> > Real examples for testing:
> >
> > f[x_] /; (Print["cond"]; x > 5) := (Print["def"]; x)
> >
> > g[x_] := (Print["def"]; x) /; (Print["cond"]; x > 5)
> >
> > h[x_] := Module[{}, Print["def"]; x /; (Print["cond"]; x > 5)]
> >
> > Usage C differs from A and B in that 'definition' is always
> evaluated,
> > and it is evaluated before 'condition'.   But I cannot see *any*
> > difference in meaning between A and B.
> >
> > Is B completely redundant?  Could someone show an example
> where an A
> > type and a B type definition behave differently?
> > Is there any situation where B can be used, but A cannot? 
> (A is more
> > general: it can be used with any pattern, while B is
> restricted to use
> > with SetDelayed and similar functions.)  Is B provided solely as a
> > more readable syntax?
> >
> > Szabolcs
> >
> > P.S.  Unfortunately usage C is "hidden" in the docs.  IMO,
> since it's
> > *meaning* (and not only syntax) is different from that of A
> and B, it
> > deserves a more prominent place in the docs.
> >
> >




  • Prev by Date: RE: The uses of Condition (/;)
  • Next by Date: Re: Re: "Accumulate" in Mathematica 6
  • Previous by thread: RE: The uses of Condition (/;)
  • Next by thread: Re: The uses of Condition (/;)