# How to use sequence of variables (Sequence) correctly?

Why does the function EER[1,2] have a different value than the function EERR[1,2]? Isn't the entry Kx[x, y] equivalent to this Kx[Sequence @@ var]? How to write EERR[Sequence @@ varr] so that it produces the same number as this EER[x_, y_]?

ClearAll["Global*"]
Kx[x_, y_] := x^2 + y;
var = {x, y};
varr = Pattern[#, Blank[]] & /@ var;
Px[x1_, y1_] := x1^3 - y1;
var1 = {x1, y1};
EER[x_, y_] := Kx[x, y] + Px[x, y];
EER[1, 2]
(*2*)
EERR[Sequence @@ varr] := Kx[Sequence @@ var] + Px[Sequence @@ var];
EERR[1, 2]
(*x^2 + x^3*)

• You need to Evaluate the RHS of EERR (in the definition).
– Alan
May 26, 2023 at 11:42
• @Alan, thanks! Could you please show May 26, 2023 at 11:46
• EERR[Sequence @@ varr] := Evaluate[Kx[Sequence @@ var] + Px[Sequence @@ var]];
– Alan
May 26, 2023 at 13:15

One recipe:

With[{var = var},
EERRM[Sequence @@ varr] := (Kx @@ var) + (Px @@ var)
]
EERRM[1,2]


(* 2 *)

or, as you used

With[{var = var},
EERRR[Sequence @@ varr] :=
Kx[Sequence @@ var] + Px[Sequence @@ var];
]

EERRR[1,2]


(* 2 *)

• Thank you very much! Сould you please explain this construct why it doesn't work without With (it's not a very obvious structure) May 26, 2023 at 12:41
• @MamMam The real problem here is not a sequence (the title therefore is misleading), but localization of variables. Usually in the definition of function one has to have same name for pattern and variable it represents, i.e. f[x_]:=x. But in your case your have f[varr_]:=var, which are different. Therefore your have to "insert" in the definition correct variables. This is most easily to do with With[] construction, which replaces all occurrences of var before evaluating its body.
– Acus
May 26, 2023 at 13:02
• Thanks for the explanation! May 26, 2023 at 13:09

Here's your definition for EERR:

EERR[Sequence @@ varr] := Kx[Sequence @@ var] + Px[Sequence @@ var]


How does that get stored as a replacement rule? You can find out by looking at DownValues:

DownValues[EERR]


{HoldPattern[EERR[x_, y_]] :> Kx[Sequence @@ var] + Px[Sequence @@ var]}

It held the right-hand side completely unevaluated, but it did evaluate the left-hand side. And notice that x and y do not show up on the right hand side. So, when it goes to apply this rule, it won't do any replacements. So, it will go on to evaluate the right-hand side as-is.

You can force evaluation:

EERR[Sequence @@ varr] := Evaluate[Kx[Sequence @@ var] + Px[Sequence @@ var]]


And now...

DownValues[EERR]


{HoldPattern[EERR[x_, y_]] :> x^2 + x^3}`