RE: Apply and Heads option
- To: mathgroup at smc.vnet.net
- Subject: [mg35225] RE: [mg35213] Apply and Heads option
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Wed, 3 Jul 2002 05:13:25 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
> -----Original Message----- > From: Ersek, Ted R [mailto:ErsekTR at navair.navy.mil] To: mathgroup at smc.vnet.net > Sent: Tuesday, July 02, 2002 8:13 AM > To: mathgroup at smc.vnet.net > Subject: [mg35225] [mg35213] Apply and Heads option > > > Hello mathgroup, > --------- > > Apply has the Heads option. > Can somebody give an example where > Apply[f, expr, Heads->True] gives a different result than > Apply[f, expr, Heads->False]. > > OR where > > Apply[f, expr, levelspec, Heads->True] gives a different > result than > Apply[f, expr, levelspec, Heads->False]. > > ---------- > Thanks, > Ted Ersek > > Ted, indeed, there is a difference, see e.g. In[100]:= Outer[Apply[ f,{h, h[x], h[x][h[x]], h[x][h[x][h[x]]], h[x][h[x][h[x][h[x]]]]},{#1}, Heads -> #2]&, Range[2,4], {True,False}]//TableForm Out[100]//TableForm= h h h[x] h[x] f[x][f[x]] h[x][f[x]] f[x][f[h[x]]] h[x][f[h[x]]] f[x][f[h[x][h[x]]]] h[x][f[h[x][h[x]]]] h h h[x] h[x] h[x][h[x]] h[x][h[x]] h[x][f[x][f[x]]] h[x][h[x][f[x]]] h[x][f[x][f[h[x]]]] h[x][h[x][f[h[x]]]] h h h[x] h[x] h[x][h[x]] h[x][h[x]] h[x][h[x][h[x]]] h[x][h[x][h[x]]] h[x][h[x][f[x][f[x]]]] h[x][h[x][h[x][f[x]]]] or In[106]:= Apply[f, h[x][h[x][h[x][h[x]]]], 3, Heads -> True] Out[106]= f[x][f[f[f[x]]]] In[107]:= Apply[f, h[x][h[x][h[x][h[x]]]], 3, Heads -> False] Out[107]= h[x][f[f[f[x]]]] As far as I can see, Apply[f, expr, levelspec, headsopt] is the same as Replace[expr, _[s___] :> f[s], levelspec, headsopt] In[147]:= Table[Replace[h[x][h[x][h[x][h[x]]]], _[s___] -> f[s], {l, ll}, Heads -> (h == 1)] == Apply[f, h[x][h[x][h[x][h[x]]]], {l, ll}, Heads -> (h == 1)], {l, 0, 3}, {ll, l, 3}, {h, 0, 1}] Out[147]= {{{True, True}, {True, True}, {True, True}, {True, True}}, {{True, True}, {True, True}, {True, True}}, {{True, True}, {True, True}}, {{True, True}}} -- Hartmut