I have defined this function
replaceIteratively[expr_, x_, list_] := Module[{n = 0}, expr /. x :> (n++; list[[n]])]
which does the following job
replaceIteratively[h[a]h[b]h[c]h[d],h,{h1,h2,h3,h4}]
(*Out: h1[a] h2[b] h3[c] h4[d]*)
replaceIteratively[h[a]h[b]h[c],h,{h1,h2,h3}]
(*Out: h1[a] h2[b] h3[c]*)
replaceIteratively[h[a]h[b]h[c,d],h,{h1,h2,h3}]
(*Out: h1[a] h2[b] h3[c,d]*)
However, it does not work when applied to the expression h*h*h*h
replaceIteratively[h*h*h*h,h,{h1,h2,h3,h4}]
(*Out: h1^4*)
I would like it to return h1 h2 h3 h4
How can I modify this function such that it works with expressions like Power[h,4]
?