       Re: If and ReplaceAll

• To: mathgroup at smc.vnet.net
• Subject: [mg102848] Re: [mg102851] If and ReplaceAll
• From: "David Park" <djmpark at comcast.net>
• Date: Sun, 30 Aug 2009 06:06:50 -0400 (EDT)
• References: <29689095.1251542418537.JavaMail.root@n11>

```It would be interesting to know your motivation for writing these kinds of
definitions. They seem to be nothing but an introduction to trouble.

ff[z_]:= a

doesn't even depend on the argument z. Instead you are grabbing an
expression from somewhere else and then entering it with a rule. Why not
write ff[c_]:=2c? Ok, it is just an example.

Why not write your second case as:

f[z_, c_] := If[z > 0, 2 c, 0]

f[z, 1]
If[z > 0, 2*1, 0]

If you want to distinguish between parameters and variables you could also
write this using SubValues:

Clear[f]
f[c_][z_] := If[z > 0, 2 c, 0]
f[z]
If[z > 0, 2*1, 0]

The problem you are having with the If statement is that it has the
Attribute HoldRest. Therefore a is not evaluated when the definition is
used. You can fix that with:

Clear[f]
a = 2 c;
f[z_] := If[z > 0, Evaluate@a, 0]
f[z] /. c -> 1

If[z > 0, 2*1, 0]

But I think it is much better to have explicit parameters and variables in
the definitions and not use some round about method of substitution.

David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/

From: wiso [mailto:gtu2003 at alice.it]

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

```

• Prev by Date: Can't reproduce a solution found in a paper using Mathematica
• Next by Date: Re: ClearAll ?? or what
• Previous by thread: Re: If and ReplaceAll
• Next by thread: turn off mirroring of ChartElements?