Skip to main content
edited tags
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

It is customary to use the following idiom to partially evaluate something inside a held expression:

f[x_] := 2 x
Block[{f}, Hold[f[1 + 1]] /. x_f :> With[{r = x}, r /; True]]

Out[]= Hold[f[2]]

This works also with builtinbuilt-in symbols, like

Block[{Abs}, Hold[Abs[1 + 1]] /. x_Abs :> With[{r = x}, r /; True]]

Out[]= Hold[Abs[2]]

However, I cannot understand the behaviour of the following

Block[{Plus}, Hold[1 + 2*2] /. x_Plus :> With[{r = x}, r /; True]]

which returns Hold[5] instead of Hold[1+4]. Can someone explain why?

It is customary to use the following idiom to partially evaluate something inside a held expression:

f[x_] := 2 x
Block[{f}, Hold[f[1 + 1]] /. x_f :> With[{r = x}, r /; True]]

Out[]= Hold[f[2]]

This works also with builtin symbols, like

Block[{Abs}, Hold[Abs[1 + 1]] /. x_Abs :> With[{r = x}, r /; True]]

Out[]= Hold[Abs[2]]

However, I cannot understand the behaviour of the following

Block[{Plus}, Hold[1 + 2*2] /. x_Plus :> With[{r = x}, r /; True]]

which returns Hold[5] instead of Hold[1+4]. Can someone explain why?

It is customary to use the following idiom to partially evaluate something inside a held expression:

f[x_] := 2 x
Block[{f}, Hold[f[1 + 1]] /. x_f :> With[{r = x}, r /; True]]

Out[]= Hold[f[2]]

This works also with built-in symbols, like

Block[{Abs}, Hold[Abs[1 + 1]] /. x_Abs :> With[{r = x}, r /; True]]

Out[]= Hold[Abs[2]]

However, I cannot understand the behaviour of the following

Block[{Plus}, Hold[1 + 2*2] /. x_Plus :> With[{r = x}, r /; True]]

which returns Hold[5] instead of Hold[1+4]. Can someone explain why?

Post Reopened by Mr.Wizard
Post Closed as "Duplicate" by Mr.Wizard
edited tags
Link
Tweeted twitter.com/#!/StackMma/status/628308257866678272
edited body
Source Link
rcollyer
  • 34.1k
  • 7
  • 94
  • 194

It is customary to use the following idiom to partially evaluate something inside a held expression:

f[x_] := 2 x
Block[{f}, Hold[f[1 + 1]] /. x_f :> With[{r = x}, r /; True]]

Out[]= Hold[f[2]]

This works also with builtin symbols, like

Block[{Abs}, Hold[Abs[1 + 1]] /. x_Abs :> With[{r = x}, r /; True]]

Out[]= Hold[Abs[2]]

However, I cannot understand the behaviour of the following

Block[{Plus}, Hold[1 + 2*2] /. x_Plus :> With[{r = x}, r /; True]]

which returns Hold[7]Hold[5] instead of Hold[1+6]Hold[1+4]. Can someone explain why?

It is customary to use the following idiom to partially evaluate something inside a held expression:

f[x_] := 2 x
Block[{f}, Hold[f[1 + 1]] /. x_f :> With[{r = x}, r /; True]]

Out[]= Hold[f[2]]

This works also with builtin symbols, like

Block[{Abs}, Hold[Abs[1 + 1]] /. x_Abs :> With[{r = x}, r /; True]]

Out[]= Hold[Abs[2]]

However, I cannot understand the behaviour of the following

Block[{Plus}, Hold[1 + 2*2] /. x_Plus :> With[{r = x}, r /; True]]

which returns Hold[7] instead of Hold[1+6]. Can someone explain why?

It is customary to use the following idiom to partially evaluate something inside a held expression:

f[x_] := 2 x
Block[{f}, Hold[f[1 + 1]] /. x_f :> With[{r = x}, r /; True]]

Out[]= Hold[f[2]]

This works also with builtin symbols, like

Block[{Abs}, Hold[Abs[1 + 1]] /. x_Abs :> With[{r = x}, r /; True]]

Out[]= Hold[Abs[2]]

However, I cannot understand the behaviour of the following

Block[{Plus}, Hold[1 + 2*2] /. x_Plus :> With[{r = x}, r /; True]]

which returns Hold[5] instead of Hold[1+4]. Can someone explain why?

Source Link
Federico
  • 2.6k
  • 16
  • 16
Loading