A couple direct ways of doing this are to use Thread
and Distribute
.
Thread[{Hold[1 + 2], Hold[3 + 4], Hold[5*6]}, Hold]
Distribute[{Hold[1 + 2], Hold[3 + 4], Hold[5*6]}, Hold]
Hold[{1 + 2, 3 + 4, 5 6}]
Hold[{1 + 2, 3 + 4, 5 6}]
The problem with Distribute
is that it distributes, so if the Hold
objects have more than one argument, you will get probably unwanted distribution.
Distribute[{Hold[1+2, x], Hold[3+4], Hold[5+6]}, Hold]
Hold[{1 + 2, 3 + 4, 5 + 6}, {x, 3 + 4, 5 + 6}]
On the other hand, the nice thing about Distribute
is that you can stick in arbitrary heads:
SetAttributes[f, HoldAll];
Distribute[{Hold[1+2], Hold[3+4], Hold[5*6]}, Hold, List, f, g]
f[g[1 + 2, 3 + 4, 5 6]]
Just like Distribute
, Thread
may not work as desired when the Hold
objects have more than one argument:
Thread[{Hold[1 + 2, x], Hold[3 + 4], Hold[5*6]}, Hold]
Thread::tdlen: Objects of unequal length in {Hold[1+2,x],Hold[3+4],Hold[5 6]} cannot be combined.
{Hold[1 + 2, x], Hold[3 + 4], Hold[5 6]}
Thread[{Hold[1 + 2, x], Hold[3 + 4, y], Hold[5*6, z]}, Hold]
Hold[{1 + 2, 3 + 4, 5 6}, {x, y, z}]
Finally, Thread
has support for a 3rd argument, although I don't think I've ever used it:
Thread[{Hold[1+2, x], Hold[3 + 4], Hold[5*6]}, Hold, 1]
Hold[{1 + 2, Hold[3 + 4], Hold[5 6]}, {x, Hold[3 + 4], Hold[5 6]}]
Summarizing, I find Distribute
and Thread
useful when working with Hold
objects.
Sequence
intoHold
. $\endgroup$Distribute[{Hold[1 + 2], Hold[3 + 4], Hold[5*6]}, Hold]
. $\endgroup$Distribute
overThread
there? $\endgroup$