The code
Table[i, {i, 5}]
produces the output
{1, 2, 3, 4, 5}
whereas the code
x = {i, 5};
Table[i, x];
produces an error, namely
Table::itform: Argument x at position 2 does not have the correct form for an iterator.
Why, and how do I fix this?
Answer
Table
has attribute HoldAll
. This means its arguments are left unevaluated:
Attributes[Table]
(* {HoldAll, Protected} *)
Using an Evaluate
will force the evaluation order to be as you desire:
x = {i, 5};
Table[i, Evaluate@x]
(* {1, 2, 3, 4, 5} *)
Comments
Post a Comment