4
$\begingroup$

What is wrong and why I cannot get z = 0; Apply[Table, {z++, {i, 0, 9}}] to output exactly like z = 0; Table[z++, {i, 0, 9}]?

$\endgroup$
3
  • 1
    $\begingroup$ try z = 0; Apply[Table, Unevaluated@{z++, {i, 0, 9}}]? $\endgroup$
    – kglr
    Jun 26, 2021 at 13:00
  • 2
    $\begingroup$ To elaborate on @kgir's comment, Mathematica first evaluates {z++, {i,0,9}} to {0, {i, 0, 9}} and only then applies Table. Unevaluated postpones that evaluation. $\endgroup$
    – bbgodfrey
    Jun 26, 2021 at 13:03
  • 2
    $\begingroup$ Or just Table @@ {i, {i, 0, 9}} $\endgroup$
    – Bob Hanlon
    Jun 26, 2021 at 14:10

1 Answer 1

6
$\begingroup$

You don't get the same output because when you evaluate z = 0; Apply[Table, {z++, {i, 0, 9}}] z++ evaluates to 0 before the head gets replaced. Table has the attribute HoldAll which prevents the arguments to be evaluated prematurely.

If you want to maintain some arguments in an unevaluated form, waiting for the correct head to be applied, you should wrap them in Hold instead:

z = 0; Apply[Table, Hold[z++, {i, 0, 9}]]
(*{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}*) 
$\endgroup$
1
  • 1
    $\begingroup$ I upvoted because of the second sentence. This is the critical part. $\endgroup$ Jun 26, 2021 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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