3
$\begingroup$

This question already has an answer here:

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?

$\endgroup$

marked as duplicate by acl, Sjoerd C. de Vries, whuber, Oleksandr R., Artes Apr 22 '13 at 18:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    $\begingroup$ Try this, Table@@{i,x}. If you check the Attributes of Table you will notice that has the HoldAll attribute so the x will not be evaluated. $\endgroup$ – Spawn1701D Apr 22 '13 at 1:56
  • $\begingroup$ You can also say Table[i,x//Evaluate] but I find it a little ugly :P $\endgroup$ – Spawn1701D Apr 22 '13 at 1:57
  • $\begingroup$ Try With[{x = {i, 5}}, Table[i, x]]. $\endgroup$ – chyanog Apr 22 '13 at 9:47
  • $\begingroup$ I would like to petition that this question be un- marked as duplicate. This question is REALLY about how you pass an entire iterator form as a value to a function. The other question is about getting a built in function to correctly handle a new, user-specified iterator form. They are just superficially similar in that they both (incidentally) use the iterator argument to Table as an example problem for the actual underlying question. $\endgroup$ – billc Aug 20 '15 at 5:13
5
$\begingroup$

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} *)
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.