MathGroup Archive 1998

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

Search the Archive

Re: Return inside Do, For and While



Hi Sean, I don't have the answer, but I do have some more questions to
add: The BOOK says (3rd edition, p. 333): Return[expr]        return
the value expr exiting all procedures and loops in a function

Here is an example how this works for loops:

In[1]:= f1:=While[True,While[True,While[True,Return[1]]]]

In[2]:= f1

Out[2]= 1

And here is one to show how it doesn't

In[3]:= f2:=Do[Print[k];Do[Return[n],{n,10}],{k,11,12}]


In[4]:= f2

11
12

One more that works kind of (the thing is that I don't understand how it
should work);

In[5]:= f3:=Do[If[True,Return[n]],{n,10}]

In[6]:= f3

Out[6]= 1

and here one that doesn't:

In[7]:= f5:=While[True,Do[If[True,Return[n]],{n,10}]]

In[8]:= f5

Out[8]= $Aborted

(Didn't stop)

Coming back to the message of the BOOK, one should ask what is a
function and what is a procedure.

Let me start with procedure.
The index refers to
p 43: as far as I can see, the word procedure refers here to a compound
expression. This guess is backed by the example on p 334 (last but one
paragraph).
p 357: here obviously procedure is used for a Module. p 355: I suppose
procedure refers to both the statement "This means that you can
explicitly give a "program" as the expression to compile" (inner quotes
by the BOOK) as well to the example where such a "program" is given by
a Module.
p 107: Here the BOOK states first that functions are procedures and
takes it partially back saying that functions are "essentially
procedures" (quotes by me).

To sum up, I propose the theory that the word procedure in the BOOK is
used in a not strictly formal way enclosing functions (still to be
analyzed), loop constructs (see p 333 third paragraph), compound
expressions (so as a special case expressions) so (see page 228)
everything.

Now as to functions, page 228, suggests that every expression is a
function (second paragraph) but maybe that is too wide a definition.
(There it says that an expression f[x,y] might be used to represent a
mathematical function.) But at least everything in A.10 is one (see
index, Functions, where it says "Functions, alphabetical listing of all
built-in, 1023"). If you think I'm nasty (naughty? sorry to say that my
English is not very good), I'm willing to reduce myself to less
objects: p 328: Table and Sum are functions ("... functions like Table
and Sum."). Do is one ("..just like in other iteration functions..") p
329: Everything that can be used as first argument in a function (?)
like Nest or FixedPoint is a function. ("Nest allows to apply a
function a specified number of times.")
I think this is enough to back up the examples to follow:

Example 1:

In[1]:= Do[Return[n],{n,2}]

Out[1]= 1

My interpretation: Do is a function, it acts like a function composed of
a compound statement as for example in

In[2]:= f:=(Return[1];Return[2])

In[3]:= f

Out[3]= 1

 Example 2:

In[4]:= f1:=While[True,While[True,While[True,Return[1]]]]

In[5]:= f1

Out[5]= 1

My interpretation: f1 is a function, Return "exits all loops".

Example 3:

In[6]:= While[True,While[True,Return[1]]]

Out[6]= Return[1]

No idea what happens, same with

In[7]:= While[True,While[True,While[True,Return[1]]]]

Out[7]= Return[1]

Example 4:

In[8]:= f2:=Return[1]; f3:={f2,2}

In[9]:= f3

Out[9]= {1,2}

My interpretation: Return exits f2, but cannot (why?) exit List, same as
in

In[10]:= Table[Return[n],{n,2}]

Out[10]= {Return[1],Return[2]}

Examples where I do not want to interpret the result:


In[11]:= g1:=Return[1];g2:=Return[2];g3:=(g1;g2)

In[12]:= g3

Out[12]= 2

-------------------

In[13]:= g4:=Return[Return[1]];g5:=g4

In[14]:= {g4,g5}

Out[14]= {Return[1],1}

Compare with

In[15]:= g6:=Return[1];g7:=g6

In[16]:= {g6,g7}

Out[16]= {1,1}

--------------------------

In[17]:= NestList[Return,1,2]

Out[17]= {1,Return[1],Return[Return[1]]}

--------------------------

In[18]:= NestList[(Return[1];#)&,1,2]

Out[18]= {1,Return[1],Return[1]}

I think that should be enough to illustrate the problem. By the way, I
did read the phrase (Help on Return) "Return takes effect as soon as it
is evaluated, even if it appears inside other functions.", but it only
added to my confusion.

Let me end with a statement and my own rules about Return:

Statement 1: As far as I remember I used Return twice and needed it
never.

Statement 2: It was immense fun to "analyze" Return, hopefully nobody is
offended.

Statement 3: The BOOK is quite clear about those "procedural
constructs": avoid them (see p xiv, Writing Programs).

My rules about Return:

1. Never use Return.
2. If you have to use it, still don't use it.

Jürgen


-----Original Message-----
From: Sean Ross <seanross@worldnet.att.net> To: mathgroup@smc.vnet.net
Subject: [mg12604] [mg12568] Return inside Do, For and While


>I am going through the Power Programming book by Wagner and have a
>question about Return[value].
>
>According to the documentation, it is supposed to cause a "function" to
>return a value.  I have always used it to exit and return a value from
>a Module or With.  Because I almost never use Do, For or While except
>when following a textbook, I had never tried to use Return from inside
>a loop.
>
>For example, consider the following code that tests a list of integers
>to see if they are all odd:
>
>allOdd[x_List]:=Module[{i=1},
>
> Do[If[!OddQ[x[[i]]],
>       Return[False]];
>     i++,
> {Length[x]}];
>
> Return[True]]
>
>This function returns True no matter what the list is.  Consider the
>following modification:
>
>allOdd[x_List]:=Module[{i=1},
>    If[!Do[If[!OddQ[x[[i]]],
> Return[False]];
> i++,
>    {Length[x]}],
>Return[False]];
>
> Return[True]]
>
>This makes it clear that Wolframs definition of a "function" includes
>loops (Do, While, For).
>
>Does anyone know what other built in objects qualify as "functions" for
>purposes of using Return?
>




  • Prev by Date: Re: A Bug / Feature
  • Next by Date: Re: Finding the Path to the Active Notebook
  • Prev by thread: Re: Return inside Do, For and While
  • Next by thread: Math. 3.0 on 95 -- Printing problems: Fonts size?