3

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?

Daniel McLaury
  • 405
  • 2
  • 9
  • 2
    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. – Spawn1701D Apr 22 '13 at 01:56
  • You can also say Table[i,x//Evaluate] but I find it a little ugly :P – Spawn1701D Apr 22 '13 at 01:57
  • Try With[{x = {i, 5}}, Table[i, x]]. – chyanog Apr 22 '13 at 09:47
  • 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.

    – billc Aug 20 '15 at 05:13

1 Answers1

5

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} *)
VF1
  • 4,702
  • 23
  • 31