Skip to main content
Rollback to Revision 1
Source Link
Szabolcs
  • 238.9k
  • 32
  • 653
  • 1.3k

You are looking for partial evaluation. Perhaps, there is a shorter way, but here is one (rather subtle):

Clear[evalAtPattern] $evalAtPattern[expr_evalAtPattern[expr_, p_] := expr /. pt : p :> With[{pp = pt}, pp /; True]; Clear[formula]; formula[x_List] := evalAtPattern[ Defer[(x[[1]] - x[[2]]) + (x[[3]] - x[[4]])], HoldPattern[x[[_]]]]; 

Now,

formula[{1, 2, 3, 4}] (* (1 - 2) + (3 - 4) *) 

And the output code can be executed. The solution combined injecting evaluated pieces inside held expressions via Trott - Strzebonski technique, and the use of Defer.

You are looking for partial evaluation. Perhaps, there is a shorter way, but here is one (rather subtle):

Clear[evalAtPattern] $evalAtPattern[expr_, p_] := expr /. pt : p :> With[{pp = pt}, pp /; True]; Clear[formula]; formula[x_List] := evalAtPattern[ Defer[(x[[1]] - x[[2]]) + (x[[3]] - x[[4]])], HoldPattern[x[[_]]]]; 

Now,

formula[{1, 2, 3, 4}] (* (1 - 2) + (3 - 4) *) 

And the output code can be executed. The solution combined injecting evaluated pieces inside held expressions via Trott - Strzebonski technique, and the use of Defer.

You are looking for partial evaluation. Perhaps, there is a shorter way, but here is one (rather subtle):

Clear[evalAtPattern] evalAtPattern[expr_, p_] := expr /. pt : p :> With[{pp = pt}, pp /; True]; Clear[formula]; formula[x_List] := evalAtPattern[ Defer[(x[[1]] - x[[2]]) + (x[[3]] - x[[4]])], HoldPattern[x[[_]]]]; 

Now,

formula[{1, 2, 3, 4}] (* (1 - 2) + (3 - 4) *) 

And the output code can be executed. The solution combined injecting evaluated pieces inside held expressions via Trott - Strzebonski technique, and the use of Defer.

added 1 characters in body
Source Link
Szabolcs
  • 238.9k
  • 32
  • 653
  • 1.3k

You are looking for partial evaluation. Perhaps, there is a shorter way, but here is one (rather subtle):

Clear[evalAtPattern] evalAtPattern[expr_$evalAtPattern[expr_, p_] := expr /. pt : p :> With[{pp = pt}, pp /; True]; Clear[formula]; formula[x_List] := evalAtPattern[ Defer[(x[[1]] - x[[2]]) + (x[[3]] - x[[4]])], HoldPattern[x[[_]]]]; 

Now,

formula[{1, 2, 3, 4}] (* (1 - 2) + (3 - 4) *) 

And the output code can be executed. The solution combined injecting evaluated pieces inside held expressions via Trott - Strzebonski technique, and the use of Defer.

You are looking for partial evaluation. Perhaps, there is a shorter way, but here is one (rather subtle):

Clear[evalAtPattern] evalAtPattern[expr_, p_] := expr /. pt : p :> With[{pp = pt}, pp /; True]; Clear[formula]; formula[x_List] := evalAtPattern[ Defer[(x[[1]] - x[[2]]) + (x[[3]] - x[[4]])], HoldPattern[x[[_]]]]; 

Now,

formula[{1, 2, 3, 4}] (* (1 - 2) + (3 - 4) *) 

And the output code can be executed. The solution combined injecting evaluated pieces inside held expressions via Trott - Strzebonski technique, and the use of Defer.

You are looking for partial evaluation. Perhaps, there is a shorter way, but here is one (rather subtle):

Clear[evalAtPattern] $evalAtPattern[expr_, p_] := expr /. pt : p :> With[{pp = pt}, pp /; True]; Clear[formula]; formula[x_List] := evalAtPattern[ Defer[(x[[1]] - x[[2]]) + (x[[3]] - x[[4]])], HoldPattern[x[[_]]]]; 

Now,

formula[{1, 2, 3, 4}] (* (1 - 2) + (3 - 4) *) 

And the output code can be executed. The solution combined injecting evaluated pieces inside held expressions via Trott - Strzebonski technique, and the use of Defer.

Source Link
Leonid Shifrin
  • 115.8k
  • 16
  • 341
  • 435

You are looking for partial evaluation. Perhaps, there is a shorter way, but here is one (rather subtle):

Clear[evalAtPattern] evalAtPattern[expr_, p_] := expr /. pt : p :> With[{pp = pt}, pp /; True]; Clear[formula]; formula[x_List] := evalAtPattern[ Defer[(x[[1]] - x[[2]]) + (x[[3]] - x[[4]])], HoldPattern[x[[_]]]]; 

Now,

formula[{1, 2, 3, 4}] (* (1 - 2) + (3 - 4) *) 

And the output code can be executed. The solution combined injecting evaluated pieces inside held expressions via Trott - Strzebonski technique, and the use of Defer.