MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Mathematica daily WTF

On 12/23/2010 12:55 AM, kj wrote:
> In<ies9t9$aa9$1 at>  Andrzej Kozlowski<akoz at>  writes:
>> Actually, there is nothing puzzling about this at all.

You can only judge if something puzzles you, yourself.

The general topic of naming and binding has been seriously mishandled 
over the years by WRI.  Hence the original Block was found lacking and
so Module was introduced.  Module does not really do lexical scope since
the names it makes up are accessible global symbols like foo$123.

There are other issues, which have often been debated in the context of
Lisp, a language from which Mathematica has borrowed some ideas.

For example,
Sin=1234  is forbidden since Sin is "protected".

Block[{Sin=1234}, Sin]   returns 1234  since the local variable Sin is 
different from the global Sin, and is not protected, explaining this 
WTF, perhaps more simply.

Indeed, Block[{Sin = 1234}, Sin [Pi]]  returns 1234[Pi]

but wait, there's more.

  Block[{Sin}, Sin[Pi]]
returns 0, because local Sin evaluates to global Sin which then
is the usual sine function.

Module[{Sin}, Sin]
returns Sin$1317    or something like that.

  Block[{Sin = 1234}, Sin [Pi]]  returns 1234[Pi]

Now of course it will not puzzle Andrzej, but here is a little
test that you can run on (a) your brain. (b) your Mathematica.

Block[{Sin = notSin}, Block[{Sin}, Sin[Pi]]]
Block[{Sin = notSin}, Module[{Sin}, Sin[Pi]]]
Module[{Sin = notSin}, Module[{Sin}, Sin[Pi]]]
Module[{Sin = notSin}, Block[{Sin}, Sin[Pi]]]

You might consider as possibilities:
1. 0
2. notSin[Pi]
3. Sin$1325[Pi]   your number may vary//

In Common Lisp, the question of whether a local symbol
(e.g. sort of like a Module variable)
  has the global property list of the symbol that
prints the same is resolved as: No.
  does it have the global property of the function definition of
the symbol with the same name:  No.

However, if you use a name as a function, e.g. Sin, then regardless
of local value bindings, you get the function.
(let((sin 1234))(values (sin pi) sin))
returns the values 0.0 and 1234.    (actually, not 0.0 but 10^(-16) apx)

So Common Lisp is a 2-lisp in this respect. functions and values
are separate. Scheme variety is a 1-lisp requires function=value.

It seems that if Mathematica were a lisp, it would be closer to a 
1-lisp, though if it were really lisp, it would have a much different 
and far simpler evaluation mechanism. Lisp has "quote" and "eval". 
Instead of all the Hold, Release, Eval, and friends.  In Lisp, the 
explicit use of eval is almost never used except by novices who are 
usually making mistakes. Whether Mathematica's evaluation is the
right thing is presumably now moot.  Remember that "evaluate until it 
stops" makes x=x+1 hurt.


  • Prev by Date: Re: Combining Slider and SetterBar in Manipulate
  • Next by Date: Re: Generic Button/Palette design pattern?
  • Previous by thread: Re: Mathematica daily WTF
  • Next by thread: Re: Mathematica daily WTF