MathGroup Archive 2001

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

Search the Archive

Re: Re: exchanging 2 elements in a list (problem with Block)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg27790] Re: [mg27631] Re: exchanging 2 elements in a list (problem with Block)
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Sat, 17 Mar 2001 05:41:14 -0500 (EST)
  • References: <933FF20B19D8D411A3AB0006295069B0286951@dassne02.darmstadt.dsh.de> <3AAF97B7.8494FB81@cs.berkeley.edu>
  • Sender: owner-wri-mathgroup at wolfram.com

 Richard, Helmut,
A few remarks:

USES OF BLOCK WHERE MODULE DOES NOT WORK
(all except 4 use only the ability of Block to temporarily set aside
definitons - only  4  introduces a temporary definition)

1 ---------------

    i=j;

    Table[i^j, {j,3}]

        {1, 4, 27}

    Module[{i},Table[i^j, {j,3}]]

        {i$1049, i$1049^2, i$1049^3}

    Block[{i},Table[i^j, {j,3}]]

        {j, j^2, j^3}

2 ------------

    Block[{Dot},Sort/@({q,p}.{x,y})]

        p*x + q*y

3 ------------

    Thread[Length[{{1,2,3},{4,5}}]]

        Thread::normal: Nonatomic expression expected at position 1 in
Thread[2].

        Thread[2]

    Block[{Length}, Thread[Length[{{1,2,3},{4,5}}]]]

        {3, 2}

 4 -------------

       Block[{$DisplayFunction = Identity},
         Plot[Sin[x],{x,0,2Pi}]
        ]


        Show[%]

5 -----------

    x=1;

    Module[{x},Integrate[x^2,x]]

        x$3^3/3

    Block[{x},Integrate[x^2,x]]

        1/3

BAD USE OF  MODULE

    $ModuleNumber= 1;
    Module[{x=a}, x +x$1]

        2*a

BAD USE OF  FUNCTION

    Function[y,Function[x,x+y]][x$]

    Function[x$, x$ + x$]


NOTES

-- In general don't directly introduce symbols like  x$, x$123 -- leave this
to Mathematica


-- Be careful whenever users or evaluation can introduce the symbols over
which you have no control - this was the problem with the original example
of the undesirable behaviour of  Block

-- We get some protection from interactions amongse scoping constructs,
Module, With, Function, Passing using named patterns (n:pattern) ...  but
beware

**** Nothing can protect us from the consequences of replacement rules
except care ****

Clear[x,y]

Function[x, Function[x, x+y]][y]

Function[x, x + y]

Function[y, Function[x, x+y]][x]

Function[x$, x$ + 1]

But

Function[x, x+y] /.x->y

Function[y, y + y]

Function[x, x+y] /.y->x

Function[x, x + x]


Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

----- Original Message -----
From: "Richard Fateman" <fateman at cs.berkeley.edu>
To: mathgroup at smc.vnet.net
Subject: [mg27790] Re: [mg27631] Re: exchanging 2 elements in a list (problem with
Block)




"Wolf, Hartmut" wrote:
>
> Dear Allan,
>
> you'r right, a general problem with, not of, Block. Block indeed does
exacly
> what it is supposed to do.


To declare a bug to be a feature is not to eliminate it. Most people
who use Block, do so at the risk of exactly the problem pointed out.
Module was created (also in a hackish way that can be broken if
you use variables like x$$4, I think), to provide a better but
still broken version of programming language scope in Mathematica.


>
> This kind of problems is shared e.g. by all early Lisp dialects having
been
> discussed extensively, and being the reason for generally converting to
> lexical scoping.

Early lisps had dynamic scope by default in interpreted code.  Dynamic
scope is not, in my view, a bug.  In fact, the ANSI standard common
lisp provides facilities for having dynamic scope  (defvar, special
variable declarations).  This is
not the same as the Mathematica Block bug, which so far as I can tell
has
no use and should be eliminated from the language.  If there is a
legitimate
example in which the semantics of Block (rather than the semantics of
Module) make sense, I would be interested in learning it.
>  That it is present at all in Mathematica presumably has
> historical reasons.

Um, it was there before Module, certainly. That it is STILL there
I don't know.

> However it also has certain advantages, and I'm happy to
> have it at my disposal.

Can you name one? I can defend it only indirectly, in that Module
is ALSO done wrong, and you have a choice of the bugs/inefficiency in
Module
or the gross bug in Block.  If Module were done as in lambda-binding,
in Lisp then I can't think of a reason.

> We have to be wary however, and routinely better use
> Module. So a first thought when seeing a Block might be: "will it work
with
> Module?"

Yep. I agree 100%
>
> A method to make Block work with Swap1 (not seriously meant for this case)
> is to restrict evaluation to where it is needed:
>
>
> In[18]:= Clear[s]
> In[19]:= lazySwap[m_,i_,j_] :=
>            Evaluate /@ Block[{s = Unevaluated /@ m},
>                               s[[{i, j}]] = s[[{j, i}]];
>                               s]
>
> In[20]:= lazySwap[{a, s},1,2]
> Out[20]= {s,a}
>
> Finally a note I would like to add to Richard's problem (after all has
been
> said by you and Jens-Peer Kuska, to mention). His pitfall is common to all
> having been absent from Mathematica for a while, and extensively
programming
> in C or a similiar language, then misconstruing a definition in Mathemtica
> for a "function procedure". A definition is a term rewrite rule, and there
> is no such thing as a "formal parameter" (with the meaning of a storage
> location behind).
> Pure lambda calculus wouldn't rise the idea, and Mathematica comes closer
to
> that than to C.

Unfortunately, the Mathematica book assists in this "thought crime" of
viewing
rules as function definitions.

>
> How to approach call by reference semantics has been shown by Jens-Peer
and
> David B. Wagner in his Book "Power Programming with Mathematica, the
Kernel"
>
> http://www.amazon.com/exec/obidos/ASIN/007912237X
> devotes all of §4.4 "Parameter-Passing Semantics" (of chapter 4
"Procedural
> Programming") to the theme.


Thanks for  the reference.
RJF

>
> With kind regards, yours
> Hartmut
>



  • Prev by Date: Re: Re: exchanging 2 elements in a list (problem with Block)
  • Next by Date: Re: "just a bug" !; Mathematica 4
  • Previous by thread: Re: Re: exchanging 2 elements in a list (problem with Block)
  • Next by thread: Re: [SYS #14934] (general) serious newsgroup problem