MathGroup Archive 2001

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

Search the Archive

Re: scope all wrong? in Mathematica 4.1

> Consider the mathematica definition
> rr[x_] := Block[{x}, x = 5; Print["x is ", x]]
>   what do you expect here?

A disaster ? a complete crash of the system ?
The destruction of Mathematica's memory management ?
Future unpredicted results form Mathematica ?
(That happens in early versions of Mathematica in such a case)

> 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.

Wow ! Mathematica is so smart -- one of the most clever 
programs I have ever seen.

You type complete nonsense and assign a value to a pattern
and you only get an error message -- it is great !

You should definitely try to assign a value to a pattern
to Mathematica version 1.2 !

A pattern stand for "all things like ..." and you can't
say "all things" are 5.

> 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.

A pattern is *not* a variable on the stack of a C-program !
A pattern is a general form and can't have a value.

> 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

My Mathematica 4.1 prints nothing. 

> If we do this:
> x =400
> uu[70]
>      x is 5  printed
> the value returned is 70
> and the global x is 5.

This is absolutely correct. What whould you expect from

uu[canBeAnyExpression_?((x = 5; Print["x is ", x]; True) &)] := x

(?) the behaviour that you report ! That you just like to say 
that "any expression" has the name "x" on the right hand side
can't affect the transformation rule.

> 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.

No ! You have typed all you examples but you have not cleared uu and x
before you enter the last example:


x =.;
Module[{x}, uu[x_?((x = 5; Print["x is ", x]; True) &)] := x;
  Print["in Module, x is ", x]]

and your global x is unchanged. 

> The reason this all came up is in correspondence suggesting
> that programs in one computer algebra system could be
> translated into another. 

If you have a Mathematica code, there is no reason 
to translate it to an other system.
But is a noble idea to translate the code from
other systems to Mathematica.

> 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).

And *I* wonder how much of your work depends on such 
sloppy "observations".
Please be a bit more careful *before* you make a wild
guesses about the internal structure of Mathematica !


  • Prev by Date: Re: Fourier: Execution time for real and complex arguments
  • Next by Date: Re: scope all wrong? in Mathematica 4.1
  • Previous by thread: scope all wrong? in Mathematica 4.1
  • Next by thread: Re: scope all wrong? in Mathematica 4.1