Skip to main content
added link
Source Link
Ajasja
  • 13.8k
  • 3
  • 47
  • 105

All you need is to ensure that the replacing is done before evaluation, including the LHS of the replace rule. Here is one way to achieve that, which I think is probably the most straightforward:

x = 1;
ReleaseHold[Hold[x + 1] /. HoldPattern[x] -> y]

This will also work for your second example:

ReleaseHold[Hold[f[x]] /. HoldPattern[x] -> {3, 2}]

Using Block for similar tasks is often more elegant, but as you have learned needs to be used with some care. I usually prefer solutions which more explicitly state what they try to achieve and here what you want to do is to change the usual evaluation order which I think can be read clearly from the above lines. Here is another way suggested by Mr.Wizard which is also more elegant and achieves the same thing, again with making use of some evaluation automatisms which might not be obvious to every reader:

Unevaluated[f[x]] /. HoldPattern[x] -> {3, 2}

For more details I'd recommend to have look at "tutorial/Evaluation""tutorial/Evaluation" in the documentation which I consider a "must know" for everyone trying to get serious work done with Mathematica...

All you need is to ensure that the replacing is done before evaluation, including the LHS of the replace rule. Here is one way to achieve that, which I think is probably the most straightforward:

x = 1;
ReleaseHold[Hold[x + 1] /. HoldPattern[x] -> y]

This will also work for your second example:

ReleaseHold[Hold[f[x]] /. HoldPattern[x] -> {3, 2}]

Using Block for similar tasks is often more elegant, but as you have learned needs to be used with some care. I usually prefer solutions which more explicitly state what they try to achieve and here what you want to do is to change the usual evaluation order which I think can be read clearly from the above lines. Here is another way suggested by Mr.Wizard which is also more elegant and achieves the same thing, again with making use of some evaluation automatisms which might not be obvious to every reader:

Unevaluated[f[x]] /. HoldPattern[x] -> {3, 2}

For more details I'd recommend to have look at "tutorial/Evaluation" in the documentation which I consider a "must know" for everyone trying to get serious work done with Mathematica...

All you need is to ensure that the replacing is done before evaluation, including the LHS of the replace rule. Here is one way to achieve that, which I think is probably the most straightforward:

x = 1;
ReleaseHold[Hold[x + 1] /. HoldPattern[x] -> y]

This will also work for your second example:

ReleaseHold[Hold[f[x]] /. HoldPattern[x] -> {3, 2}]

Using Block for similar tasks is often more elegant, but as you have learned needs to be used with some care. I usually prefer solutions which more explicitly state what they try to achieve and here what you want to do is to change the usual evaluation order which I think can be read clearly from the above lines. Here is another way suggested by Mr.Wizard which is also more elegant and achieves the same thing, again with making use of some evaluation automatisms which might not be obvious to every reader:

Unevaluated[f[x]] /. HoldPattern[x] -> {3, 2}

For more details I'd recommend to have look at "tutorial/Evaluation" in the documentation which I consider a "must know" for everyone trying to get serious work done with Mathematica...

added 23 characters in body
Source Link
Albert Retey
  • 23.6k
  • 60
  • 104

All you need is to ensure that the replacing is done before evaluation, including the LHS of the replace rule. Here is one way to achieve that, which I think is probably the most straightforward:

x = 1;
ReleaseHold[Hold[x + 1] /. HoldPattern[x] -> y]

This will also work for your second example:

ReleaseHold[Hold[f[x]] /. HoldPattern[x] -> {3, 2}]

Using Block for similar tasks is often more elegant, but as you have learned needs to be used with some care. I usually prefer solutions which more explicitly state what they try to achieve and here what you want to do is to change the usual evaluation order which I think can be read clearly from the above lines. Here is another way suggested by Mr.Wizard which is also more elegant and achieves the same thing, again with making use of some evaluation automatisms which might not be obvious to every reader:

Unevaluated[f[x]] /. HoldPattern[x] -> {3, 2}

For more details I'd recommend to have look at "tutorial/Evaluation" in the documentation which I consider a "must know" for everyone trying to get serious work done with Mathematica...

All you need is to ensure that the replacing is done before evaluation, including the LHS of the replace rule. Here is one way to achieve that, which I think is probably the most straightforward:

x = 1;
ReleaseHold[Hold[x + 1] /. HoldPattern[x] -> y]

This will also work for your second example:

ReleaseHold[Hold[f[x]] /. HoldPattern[x] -> {3, 2}]

Using Block for similar tasks is often more elegant, but as you have learned needs to be used with some care. I usually prefer solutions which more explicitly state what they try to achieve and here what you want to do is to change the usual evaluation order which I think can be read clearly from the above lines. Here is another way which is also more elegant and achieves the same thing, again with making use of some evaluation automatisms which might not be obvious to every reader:

Unevaluated[f[x]] /. HoldPattern[x] -> {3, 2}

For more details I'd recommend to have look at "tutorial/Evaluation" in the documentation which I consider a "must know" for everyone trying to get serious work done with Mathematica...

All you need is to ensure that the replacing is done before evaluation, including the LHS of the replace rule. Here is one way to achieve that, which I think is probably the most straightforward:

x = 1;
ReleaseHold[Hold[x + 1] /. HoldPattern[x] -> y]

This will also work for your second example:

ReleaseHold[Hold[f[x]] /. HoldPattern[x] -> {3, 2}]

Using Block for similar tasks is often more elegant, but as you have learned needs to be used with some care. I usually prefer solutions which more explicitly state what they try to achieve and here what you want to do is to change the usual evaluation order which I think can be read clearly from the above lines. Here is another way suggested by Mr.Wizard which is also more elegant and achieves the same thing, again with making use of some evaluation automatisms which might not be obvious to every reader:

Unevaluated[f[x]] /. HoldPattern[x] -> {3, 2}

For more details I'd recommend to have look at "tutorial/Evaluation" in the documentation which I consider a "must know" for everyone trying to get serious work done with Mathematica...

added some remarks and examples
Source Link
Albert Retey
  • 23.6k
  • 60
  • 104

All you need is to ensure that the replacing is done before evaluation, including the LHS of the replace rule. Here is one way to achieve that, which I think is probably the most straightforward:

x = 1;
ReleaseHold[Hold[x + 1] /. HoldPattern[x] -> y]

This will also work for your second example:

ReleaseHold[Hold[f[x]] /. HoldPattern[x] -> {3, 2}]

Using Block for similar tasks is often more elegant, but as you have learned needs to be used with some care. I usually prefer solutions which more explicitly state what they try to achieve and here what you want to do is to change the usual evaluation order which I think can be read clearly from the above lines. Here is another way which is also more elegant and achieves the same thing, again with making use of some evaluation automatisms which might not be obvious to every reader:

Unevaluated[f[x]] /. HoldPattern[x] -> {3, 2}

For more details I'd recommend to have look at "tutorial/Evaluation" in the documentation which I consider a "must know" for everyone trying to get serious work done with Mathematica...

All you need is to ensure that the replacing is done before evaluation, including the LHS of the replace rule. Here is one way to achieve that, which I think is probably the most straightforward:

x = 1;
ReleaseHold[Hold[x + 1] /. HoldPattern[x] -> y]

for more details I'd recommend to look at "tutorial/Evaluation" in the documentation...

All you need is to ensure that the replacing is done before evaluation, including the LHS of the replace rule. Here is one way to achieve that, which I think is probably the most straightforward:

x = 1;
ReleaseHold[Hold[x + 1] /. HoldPattern[x] -> y]

This will also work for your second example:

ReleaseHold[Hold[f[x]] /. HoldPattern[x] -> {3, 2}]

Using Block for similar tasks is often more elegant, but as you have learned needs to be used with some care. I usually prefer solutions which more explicitly state what they try to achieve and here what you want to do is to change the usual evaluation order which I think can be read clearly from the above lines. Here is another way which is also more elegant and achieves the same thing, again with making use of some evaluation automatisms which might not be obvious to every reader:

Unevaluated[f[x]] /. HoldPattern[x] -> {3, 2}

For more details I'd recommend to have look at "tutorial/Evaluation" in the documentation which I consider a "must know" for everyone trying to get serious work done with Mathematica...

Source Link
Albert Retey
  • 23.6k
  • 60
  • 104
Loading