Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2007

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

Search the Archive

Re: The uses of Condition (/;)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg82947] Re: The uses of Condition (/;)
  • From: magma <maderri2 at gmail.com>
  • Date: Sun, 4 Nov 2007 06:11:40 -0500 (EST)
  • References: <fghcbl$k9n$1@smc.vnet.net>

On Nov 3, 9:43 am, Szabolcs Horv=E1t <szhor... at gmail.com> wrote:
> 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.


Usage C is described in the Mathematica 6 documentation under Condition - More
information (and nowhere else I think) in the form

lhs:=Module[{vars},rhs/;test]

They say that "this usage allows local variables to be shared between
test and rhs "
On the same Documentation page , under Scope, there is a concrete
example of usage C.

f[x_] := Module[{u}, u^2 /; ((u = x - 1) > 0)]

here it is seen that the pattern (u) is a local variable.
In your examples you were using x, a global variable, so no difference
was apparent.
In C, condition is evaluated before definition, for example (please
make sure you have Remove[f] before typing this:

f[x_] := Module[{u}, (Print [u]; u^2) /; ((u = x - 1) > 0)]
f[3]
2
4
u only acquires a value after cond is evaluated and then def (Print
[u]; u^2) will be evaluated.
A and B are very similar, but A is considered a bit faster, because
evaluation stops immediately if the cond is not safisfied

Changing slightly topic:

In the same Condition documentation page we find:

{6, -7, 3, 2, -1, -2} /. x_ /; x < 0 -> w
giving
{6, w, 3, 2, w, w}
and that 's ok. The x_ stand for the individual elements in the list


But why then
{6, -7, 3, 2, -1, -2} /. x_  -> w
gives
w
instead of
{w,w,w,w,w,w}  ?

shouldn't the x_ still stand for the individual elements in the list?



  • Prev by Date: Re: Converting a number back to a symbol?
  • Next by Date: RE: Re: Can you get a package back to a notebook easily?
  • Previous by thread: Re: The uses of Condition (/;)
  • Next by thread: Re: Re: The uses of Condition (/;)