5
$\begingroup$

Edit: I'm apparently blind. Sorry! And thanks to those who answered anyway.

Apologies in advance if this is sort of a nitpicky question, but I'm generally very paranoid that I don't fully understand how Mathematica functions are behaving "under the hood." The documentation of Fold seems only to address cases of the form

Fold[f, a, {x1, x2, ...}] 

i.e. where the head of the third argument is List, and expresses the result as

f[... f[f[a, x1], x2], ...]. 

However, I've noticed that it actually appears that the result of

Fold[f, a, h[x1 ,x2, ...]] 

appears to be identical to the one where h is List in all cases that I've tested. This isn't really surprising at all, at least in the sense that it seems totally consistent with the general philosophy behind how Mathematica functions behave (there are countless other examples where functions are explicitly documented as handling arbitrary heads in the same way they handle List) -- after all, extracting a Part of any expression h[...] does not depend on the head h -- but usually in such cases this is pointedly mentioned at least once somewhere in the documentation. I want to make sure I'm not missing some other reason why Fold would behave like this (or that I'm not somehow missing where this is addressed in the documentation).

Thanks!

$\endgroup$
2
  • 1
    $\begingroup$ I’m voting to close this question because it is documentted, so the question "why it is not documented" has no meaning - "loosely documented" would be acceptable, though I think it is not possible to document such a transverse feature more precisely for each function. $\endgroup$ Commented Jan 29, 2024 at 21:38
  • $\begingroup$ Yeah I just totally missed that point in the documentation. Very silly. $\endgroup$ Commented Jan 29, 2024 at 22:33

1 Answer 1

7
$\begingroup$

This behavior is actually documented further down in the page in the "Scope" section, so you can rely on it not changing in the future.

The head of the third argument need not be List:

In[1]:= Fold[f, x, p[a, b, c, d]] Out[1]= f[f[f[f[x, a], b], c], d] 
$\endgroup$
1
  • $\begingroup$ Wow I thought I had scanned the whole doc page really thoroughly, I just missed that. $\endgroup$ Commented Jan 29, 2024 at 22:33

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.