Re: scope all wrong? in Mathematica 4.1

*To*: mathgroup at smc.vnet.net*Subject*: [mg31844] Re: [mg31827] scope all wrong? in Mathematica 4.1*From*: Andrzej Kozlowski <andrzej at tuins.ac.jp>*Date*: Fri, 7 Dec 2001 05:56:18 -0500 (EST)*Sender*: owner-wri-mathgroup at wolfram.com

This sort of arguments seem to be a regular feature on this list. I do not feel like going into detail because I remember all this having been done before more than once. There are however three general points that I would make. 1) All the "unexpected" outputs are exactly as would be expected by anyone who has a fair grasp of the Mathematica language and can be supported by the documentation. Therefore they can't be called bug, at least not in the sense that I understand the term. (This is not to say that there are no bugs in mathematica, it certainly has its fair share. Just that none of these are.) In fact the first two cases are Mathematica "standard errors", discussed in this group a countless number of times . If a Mathematica beginner came up with them I would refer him to the Mathematica Book. The other examples are more subtle but highly contrived. In the unlikely event that that a beginner would be troubled by them my reaction would be to say "why on earth do you want to do that?". 2) One can certainly argue about the good and bad points of the design of a programming languages. However, to a large extent this is a matter of what one is used to, how and how often one uses a language and of personal taste. For example, you have written on the subject of Block and Module before, and I have found that some of the things that you criticize are in my opinion among their strongest points, allowing many constructions that are difficult to achieve in other programming languages. 3) I can imagine that there may be good reasons for being interested in translating between different programming languages. I do not know much about that. However, as a matter of practice I find "translating" a bad idea. I use two languages, Mathematica and Common Lisp, the first very often and the second when really needed, and I make an effort to consciously avoid trying to translate between them since this is the surest route to inefficient programming. Both languages have their unique strengths and are ideally suited for different purposes. The Mathematica language is certainly unlike any other but I really do not see why this is a bad thing. And after all, as you yourself have written, one is always always free not to use it. Since you have already made this point what is the point you are trying to make now? Andrzej On Wednesday, December 5, 2001, at 08:51 PM, Richard Fateman wrote: > Consider the mathematica definition > > rr[x_] := Block[{x}, x = 5; Print["x is ", x]] > > what do you expect here? > > rr[z] --> x is 5 is printed. > but you might not expect > > rr[4] --> "Block::"lvsym": "Local variable specification{4} > contains 4 > which is not a symbol or an assignment to a symbol." > > If you use Module instead of Block, you get > the same. > > However, > Block[{x = 4}, Block[{x}, x = 5; Print["x is ", x]]] > > prints x is 5 > > Using pattern matching for > substituting 4 for x in rr[4] even when x is bound seems > to be counter to what most programming language designers > would expect. > > > > Now look at > > uu[x_?((x = 5; Print["x is ", x]; True) &)] := x > > Usually when one defines a program, nothing is printed. > Here, we get > x is 5 > 5 > x is 5 > > If we do this: > x =400 > uu[70] > x is 5 printed > the value returned is 70 > and the global x is 5. > > Try this: > Module[{x}, uu[x_?((x = 5; Print["x is ", x]; True) &)] := x; > Print["in Module, x is " , x]] > > The expectation in a system supporting lexical scope > with Module > is that no use of x inside the module would escape. > But it does. The global x is set to 5. > > > The reason this all came up is in correspondence suggesting > that programs in one computer algebra system could be > translated into another. If systems are semantically > "surprising", it is more difficult. I wonder how much > of mathematica internally depends on wrong scope, or how > much of the code is susceptible to bugs because of unexpected > capture of names. (I suspect that this has caused a proliferation > of package names e.g. MySecretNameSpace`x in Mathematica > routines). > > RJF > > > > Andrzej Kozlowski Toyama International University JAPAN http://platon.c.u-tokyo.ac.jp/andrzej/