Re: Question on Thread
- To: mathgroup at smc.vnet.net
- Subject: [mg64466] Re: Question on Thread
- From: albert <awnl at arcor.de>
- Date: Sat, 18 Feb 2006 02:49:50 -0500 (EST)
- References: <dt1cs8$60$1@smc.vnet.net> <dt48c7$2k1$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi, > I have never fully understood Threads sometime strange behaviour. In any > case, MapThread gives me much less trouble and can do everything Thread > can. Your example would then read: > Clear[f, g] > MapThread[(f[g[#1], #2] &), {{1, 2, 3}, {{a, 1}, {b, 2}, {c, 3}}}] > > and this does what one would expect. > > Daniel > > Matt wrote: >> Hello, >> >> Clear[f] >> Thread[(f[#1, #2] & )[{1, 2, 3}, {{a, 1}, {b, 2}, {c, 3}}]] >> >> Gives the expected output, i.e. {f[1,{a,1}], f[2,{b,2}],...} >> >> but >> >> Clear[f, g] >> Thread[(f[g[#1], #2] & )[{1, 2, 3}, {{a, 1}, {b, 2}, {c, 3}}]] >> >> gives me: >> >> f[g[{1, 2, 3}], {a, 1}], f[g[{1, 2, 3}], {b, 2}], ..., etc. >> >> whereas I would have expected: >> >> f[g[1], {a, 1}], f[g[2], {b, 2}], ..., etc. >> >> I've looked at the 'Thread' documentation and in the Mathematica Book, >> but I can't see why the second case doesn't work. this is as usual a matter of evaluation order: Since neither Thread nor MapThread evaluate their arguments, these are evaluated before the functions actually see the arguments. In the case of MapThread nothing happens with the pure function given in the first argument, in the case of Thread, Thread will see as it's argument: Out[1]= f[g[{1, 2, 3}], {{a, 1}, {b, 2}, {c, 3}}] which is what In[1]:= (f[g[#1], #2] & )[{1, 2, 3}, {{a, 1}, {b, 2}, {c, 3}}] evaluates to. To avoid this, you could use: Thread[Unevaluated[(f[#1, #2] &)[{1, 2, 3}, {{a, 1}, {b, 2}, {c, 3}}]]] which gives the expected results (at least my guess of your expectation :-). hth albert