Message Boards Message Boards

1
|
2273 Views
|
2 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Why does Apply behave differently in these two cases?

Posted 10 years ago

In[1]:= Times @@@ {Plus[1, 2], Plus[3, 4]}

Out[1]= {3, 7}

In[2]:= Times @@@ {Plus[a, b], Plus[c, d]}

Out[2]= {a b, c d}

Out[2] is what I would expect, because "Apply always effectively constructs a complete new expression and then evaluates it.". In Out[1], Plus seems to be evaluated before Apply takes effect, which seems incorrect. If it is correct, how is it explained?

POSTED BY: Freek Augustijn
2 Replies

The only difference between these two examples is that Plus[1, 2] evaluates to 3, while Plus[a, b] evaluates to itself.

Apply evaluates its arguments according to the standard evaluation procedure like most other functions in Mathematica.

In[1]:= Apply[Times, Plus[2, 3]]

Out[1]= 5

In[2]:= Apply[Times, Unevaluated[Plus[2, 3]]]

Out[2]= 6

See also Non-Standard Evaluation.

POSTED BY: Ilian Gachevski
Posted 10 years ago

Thank you Ilian, that is clear. I think the phrase "and then evaluates it" in the help text of Apply is not very clear, because in the standard evaluation procedure the leaves are evaluated first.

POSTED BY: Freek Augustijn
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract