MathGroup Archive 2009

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

Search the Archive

Re: If and ReplaceAll

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:

f[c_][z_] := If[z > 0, 2 c, 0]
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:

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  

From: wiso [mailto:gtu2003 at] 

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?