Re: If and ReplaceAll
- To: mathgroup at smc.vnet.net
- Subject: [mg102839] Re: [mg102851] If and ReplaceAll
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Sun, 30 Aug 2009 06:05:07 -0400 (EDT)
- References: <200908291033.GAA00437@smc.vnet.net>
Hi, In the case with <If> you get such result since If does not evaluate its second and third (and fourth optional) arguments - it has a HoldRest attribute. This will do almost what you want: In[1] = a = 2 c; f[z_] := If[z > 0, Evaluate[a], 0] f[z] /. c -> 1 Out[1] = If[z > 0, 2 * 1, 0] with the exception that multiplication 2*1 will only be performed once the condition will evaluate to True (the difference which in this case does not matter). In general, try to avoid such constructions in favor of explicit argument passing - reliance on global variables in functions is error-prone. Regards, Leonid On Sat, Aug 29, 2009 at 3:33 AM, wiso <gtu2003 at alice.it> wrote: > a = 2c; > ff[z_] := a; > ff[z] /. c -> 1 > > > and I got 2. Ok, now: > > a = 2 c; > f[z_] := If[z > 0, a, 0] > f[z] /. c -> 1 > > and I got: If[z > 0, a, 0] > but I want: If[z > 0, 2, 0] > > I tried to use Evaluate, FullSimplify... but nothing > > >
- References:
- If and ReplaceAll
- From: wiso <gtu2003@alice.it>
- If and ReplaceAll