Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Misprint in the Documentation?

70 views
Skip to first unread message

Alexey

unread,
Feb 21, 2011, 4:21:01 AM2/21/11
to
Hello,

On the Documentation page ref/Condition under "More information" field
we read: "lhs:=Module[{vars},rhs/;test] allows local variables to be
shared between test and rhs. You can use the same construction with
Block and With. =BB". The hyperlink at the end of the citation directs
at the example for Module:

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

In[2]:= f[0]

Out[2]= f[0]

In[3]:= f[6]

Out[3]= 25

This method in this example also works with Block:

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

In[2]:= f[0]

Out[2]= f[0]

In[3]:= f[6]

Out[3]= 25

But it is seemed to be irrelevant to With:

In[1]:= f[x_]:=With[{u},u^2/;((u=x-1)>0)]
In[2]:= f[0]
During evaluation of In[2]:= With::lvws: Variable u$ in local variable
specification {u$} requires a value. >>
Out[2]= With[{u$},RuleCondition[$ConditionHold[$ConditionHold[u$^2]],(u
$=0-1)>0]]

Is it misprint or I loose something?

Leonid Shifrin

unread,
Feb 21, 2011, 7:30:30 PM2/21/11
to
Alexey,

It is not a misprint. With is for local constants - you can not assign
values to
symbols localized by With anywhere in the body of With, including the
condition.
You can only set their values in the declaration list (first argument of
With). This
is a general feature of With, with or without the condition.

Generally, this construct - variables shared between the body and the
condition,
is very useful at times, since it allows you to preserve the standard
semantics
of Mathematica functions returning "unevaluated" when pattern is not matched
(or,
continuing with trying other rules down the definition list), even when some
significant
computation might have happened behind the scenes to establish the fact of
(non)match.

You can probably find lots of good examples for this on the mathgroup
archives, the two
I am personally aware of are

http://groups.google.com/group/comp.soft-sys.math.mathematica/browse_thread/thread/3a5ae92bda1c7511

(a bit tangential, discussion is about how to implement "nested" With macro
and preserve this feature), and

http://groups.google.com/group/comp.soft-sys.math.mathematica/browse_thread/thread/41daf904fbd366be

which seems to be directly relevant. Also, I use this technique in my post
in this thread, where I also briefly explain its advantages:

http://groups.google.com/group/comp.soft-sys.math.mathematica/browse_thread/thread/7f0195213d9d50e0/

although the thread itself is about some specific problem.

HTH.

Regards,
Leonid

Bob Hanlon

unread,
Feb 21, 2011, 7:30:52 PM2/21/11
to
Use With as follows

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

f[0]

f[0]

f[6]

25


Bob Hanlon

---- Alexey <leh...@gmail.com> wrote:

=============
Hello,

Out[3]= 25

Out[3]= 25


--

Bob Hanlon


0 new messages