Re: Mathematica daily WTF
- To: mathgroup at smc.vnet.net
- Subject: [mg114900] Re: Mathematica daily WTF
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Wed, 22 Dec 2010 02:34:57 -0500 (EST)
- References: <201012200539.AAA22695@smc.vnet.net>
Actually, there is nothing puzzling about this at all. First of all, you are confusing the issue by your assignments = Context=Context etc to local variables. This gives exactly the same = answers: Block[{Attributes, Context}, {Attributes[Context], Attributes[Attributes], Context[Context], Context[Attributes]}] {{HoldFirst,Protected},{HoldAll,Listable,Protected},System`,System`} Block[{Context}, {Attributes[Context], Attributes[Attributes], Context[Context], Context[Attributes]}] {{},{HoldAll,Listable,Protected},System`,System`} In the first case both Context and Attributes have no value inside = Block, hence they are not evaluated. Block returns {Attributes[Context], Attributes[Attributes], Context[Context], Context[Attributes]} but then these are evaluated outside Block giving the expected results. In the second case, Context has no value inside Block but Attributes = has. This means that inside Block, Attributes[Context] evaluates to {}, = but the rest are unevaluated. All the unevaluated expressions are then = evaluated outside Block, returning the expected values, but {} remains = unchanged. Nothing could be more logical. Andrzej Kozlowski On 20 Dec 2010, at 06:39, kj wrote: > I've often stated that Mathematica is unique in my experience in > that despite my having used it for over two decades, still it > happens that, on average once per session, I am deeply mystified > by something it does. > > So, with apologies to Alex Papadimoulis for plagiarizing the title > of his great website (http://thedailywtf.com), I thought I'd post > an example of one of these "daily" (as in, "each day I use > Mathematica") mystifications. > > First, by way of background, consider this slightly silly, but > perfectly valid, Mathematica expression: > > In[1]:= {Attributes[Context], Attributes[Attributes], > Context[Context], Context[Attributes]} > > Out[1]= {{HoldFirst, Protected}, {HoldAll, Listable, > Protected}, "System`", "System`"} > > No surprises here. Neither should there be any surprises from the > next one, if one groks Block scope: > > In[2]:= Block[{Attributes = Attributes, Context = Context}, > {Attributes[Context], Attributes[Attributes], Context[Context], > Context[Attributes]} > ] > > Out[2]= {{HoldFirst, Protected}, {HoldAll, Listable, > Protected}, "System`", "System`"} > > > OK, so much for background. > > What about this one? > > Block[{Context = Context}, > {Attributes[Context], Attributes[Attributes], Context[Context], > Context[Attributes]} > ] > > (The input expression is identical to the previous one, except that > only Context, not Attributes, is localized within the Block scope. > > I wont' write what this last one evaluates to, for those who may > want to work it out. In any case, it's easy enough to paste the > expression into Mathematica to see what it evaluates to. > > The following questions are for those who got it right (I usually > don't find after-the-fact rationalizations too illuminating). > > If you correctly deduced the output from the last evaluation, would > you *please* explain your reasoning to me? And how did you arrive > at the knowledge that supports this reasoning? Is it in the > documentation? > > Mystifiedly, > > ~kj > > P.S. Caveat: the outputs shown or alluded to above all come from > v. 7, unfortunately. It is possible that the results of the > experiments above will be different (and less worthy of a WTF) with > v. 8. If so, my apologies. >
- References:
- Mathematica daily WTF
- From: kj <no.email@please.post>
- Mathematica daily WTF