Re: Misprint in the Documentation?
- To: mathgroup at smc.vnet.net
- Subject: [mg116613] Re: Misprint in the Documentation?
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Mon, 21 Feb 2011 19:29:33 -0500 (EST)
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 On Mon, Feb 21, 2011 at 12:20 PM, Alexey <lehin.p at gmail.com> wrote: > 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? > >