MathGroup Archive 1996

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

Search the Archive

Re: Questions on the finer points.

  • To: mathgroup at smc.vnet.net
  • Subject: [mg4513] Re: Questions on the finer points.
  • From: sherod at boussinesq.Colorado.EDU (Scott Herod)
  • Date: Fri, 2 Aug 1996 02:22:39 -0400
  • Organization: University of Colorado at Boulder
  • Sender: owner-wri-mathgroup at wolfram.com

Well, I can do one.

In article <4t6ant$7d3 at dragonfly.wolfram.com>, Hein Hundal <hundal at vicon.net> writes:
|> There are some things in Mathematica that seem to be the same, but I 
|> suspect that they are some different.  Could someone enlighten me about 
|> the difference between the following:
|>

|> 3) Block and Module

I finally have a good feel for the difference between Blocks and Modules.
I know that Modules use lexical scoping while Blocks use dynamic scoping,
but it took a couple of examples before I understood the difference.

Example 1:

Suppose I have something like the following:

   routine 1 = [
	do something;
	some_variable = output of routine 2 ( data );
	If (routine 2 used method A) do something
	else if (routine 2 used method B) do something else;
   ];

    routine 2 (data) = [
	If (condition on data) variable is output of method A;
	else variable is output of method B;
	return variable;
    ];

Now, I would like to define a flag in routine 1 which is turned either
off or on in routine 2 depending on which method was used.  However, I
would like the flag to be local to these two routines.  If routine 1
is defined with a Module, routine 2 never sees the flag because the 
flag is local to routine 1 only.  If routine 1 is defined with a Block
routine 2 would have access to the flag because it is local to routine
1's execution history, ie, routine 1 has not closed so the flag is still
accessable.  Thus, I would like to make routine 1 a Block.  routine 2 could
be a Module.

   routine 1 = Block[{flag},
	do something;
	some_variable = output of routine 2 ( data );
	If (flag) do something
	else do something else;
   ];

    routine 2 (data) = Module[
	If (condition on data) variable is output of method A;
	    flag = True;
	else variable is output of method B;
	    flag = False;
	return variable;
    ];


Ok, why not always use Blocks as opposed to Modules?  Consider the
next example

Example 2:

    routine 1 = Block[{counter}
	while counter ...
	call routine 2;
    ]

	Lot's of calls finally to

    routine 487 = Block[
	while counter ...
    ]

Because routine 1 is defined with a Block, the local variable "counter"
is still the same way down in routine 487 potentially causing name 
collisions.

I always use Modules unless I am writing something like the first
example.  Even then I would likely use a Module and pass the value of
the flag back to the first routine.

Scott Herod

==== [MESSAGE SEPARATOR] ====


  • Prev by Date: Re:ListPlot the output of Print
  • Next by Date: Re: The minimum spanning circle problem
  • Previous by thread: Questions on the finer points.
  • Next by thread: Comparison of Mathematica on Various Computers