MathGroup Archive 2007

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

Search the Archive

The uses of Condition (/;)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg82926] The uses of Condition (/;)
  • From: Szabolcs Horvát <szhorvat at gmail.com>
  • Date: Sat, 3 Nov 2007 03:31:42 -0500 (EST)

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: Can you get a package back to a notebook easily?
  • Next by Date: Re: Setting Negatives to Zero
  • Previous by thread: Eliminate items in q equation.
  • Next by thread: RE: The uses of Condition (/;)