# What is the simplest way of applying different head to different level?

Below is the code for applying same head f to all the levels that are indicated

Apply[f, {{{{{a}}}}}, {0, 3}]


Now if we want to apply f to level 0, g to level 1, h to level 2, ..., what would be the simplest form?

Using FoldList:

lis = {{{{{a}}}}};
fAt = {{f, 0}, {g, 1}, {h, 2}, {j, 3}};
(res = FoldList[Apply[First@#2, #1, {Last@#2}] &, lis,
fAt]) // TableForm


To see the final result only, use Fold instead of FoldList.

Visualization:

TreeForm /@ {lis, res}


If you are not comfortable (yet) with using FoldList, then use the following and modify the steps as you see fit:

res2 = lis // Apply[f, #, {0}] & // Apply[g, #, {1}] & //
Apply[h, #, {2}] & // Apply[j, #, {3}] &

Last@res == res2

(* True *)


Using MapIndexed:

MapIndexed[{f, g, h, j}[[Length[#2] + 1]] @@ # &, {{{{{a}}}}, {{a}}}, {0, 3}]


f[g[h[j[{a}]]], g[h[a]]]

Using Fold and Map

 Fold[Map[#2[[1]], #1, #2[[2]]] &, {{{{{a}}}}}, {{f, {0}}, {g, {-1}}}]


f[{{{{{g[a]}}}}}]