1

I'm wondering how to do the following:

    expr = L + R;

    foo[L_, R_] := expr;

    foo[1, 2]


(* L + R *)

and have it substitute the function variables into the expression. I realize that I could create dummy variables and do it like

foo[l_, r_] := expr /. L->l /. R->r

but my actual expression has a lot of free variables, and so I would like to know if there is a simpler way.

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
user545424
  • 131
  • 3

1 Answers1

1

As noted in the comments, use Set (=) instead of SetDelayed (:=) while making sure that L and R have no value assigned:

expr = L+R
foo[L_, R_] = expr

foo[1, 2]
(* ==> 3 *)
Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
Ajasja
  • 13,634
  • 2
  • 46
  • 104
  • I went ahead and changed this, since it was community wiki. I think Evaluate would be completely equivalent to just using Set here. Hope you don't mind, please check the edit. – Szabolcs Jan 21 '14 at 19:32
  • @Szabolcs Sure, even if these were not CW I welcome good edits:) – Ajasja Jan 21 '14 at 20:13