Due to confusion of terminology on my part I asked the wrong question before.
Since it has already received other answers I shall not edit it, but instead ask the question I meant to in the first place.
Mathematica provides functions that perform a depth-first postorder traversal on an expression, including: Map, MapAll, and Scan, and functions that use such a traversal, including: Count, Cases, and Position.
It is quite direct to do this:
expr = {{1, {2, 3}}, {4, 5}};
Scan[Print, expr, {0, -1}]
1
2
3
{2,3}
{1,{2,3}}
4
5
{4,5}
{{1,{2,3}},{4,5}}
It is not as obvious how to do a depth-first preorder scan. (Simply storing then reordering the output is not adequate as it doesn't change the order in which expressions are visited.)
Scan has the property that it does not build an output expression the way that e.g. Map does, and conserves memory.
How can one do a Scan-type operation in depth-first preorder?




Caseswhich performs the depth-first preorder traversal (perhaps not optimally). In that case, this was important to avoid unpacking during the traversal performed byCases. – Leonid Shifrin Aug 8 '12 at 20:52