MathGroup Archive 2004

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

Search the Archive

Re: Where does Return return to?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg48626] Re: [mg48601] Where does Return return to?
  • From: DrBob <drbob at bigfoot.com>
  • Date: Tue, 8 Jun 2004 00:48:17 -0400 (EDT)
  • References: <200406070933.FAA10950@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Let's simplify func2 and func3 a bit further:

func2[n_]:=Block[{L=n, otherstuff},someotherstuff;expr4]
func3[n_]:=Block[{L=n, otherstuff},someotherstuff;expr4]

As you can plainly see, expr4 is the value of both functions. "someotherstuff" changes the result only if expr4 involves variables changed in "someotherstuff".

The only "possible" exception is when cond1 is False and cond2 is True, since that's the only place you have a Return. But this isn't an exception, since Return only exits Do in this case, not Block. Another reason you could get the result you're seeing is if n was less than 2; in that case the Return could never be reached.

Block[{otherstuff},
   Do[
     Print@Which[False,expr1,
         True,Return[expr2],
         True,expr3];Print@duh,
     {L,5,2,-1}];
   expr4]

Return[expr2]

duh

Return[expr2]

duh

Return[expr2]

duh

Return[expr2]

duh

expr4

Block[{otherstuff},
   Do[
     Print@Which[True,expr1,
         True,Return[expr2],
         True,expr3],
     {L,5,2,-1}];
   expr4]

expr1

expr1

expr1

expr1

expr4

Bobby

On Mon, 7 Jun 2004 05:33:29 -0400 (EDT), Ray Koopman <koopman at sfu.ca> wrote:

> Consider the following three functions,
> which I thought would be equivalent.
>
> func1[n_] := Block[{L = n, otherstuff},
>              While[L > 1,
>                    Which[cond1, expr1,
>                          cond2, Return[expr2],
>                          True,  expr3];
>                    L--];
>              expr4]
>
> func2[n_] := Block[{L = n, otherstuff},
>              While[Which[cond1, expr1,
>                          cond2, Return[expr2],
>                          True,  expr3];
>                    --L > 1];
>              expr4]
>
> func3[n_] := Block[{otherstuff},
>              Do[Which[cond1, expr1,
>                       cond2, Return[expr2],
>                       True,  expr3],
>                 {L,n,2,-1}];
>              expr4]
>
> func1 returns expr2 or expr4, as desired,
> but both func2 and func3 always return expr4.
>
> In each function, nothing changes if I replace the Which by
>
>    If[cond1, expr1, If[cond2, Return[expr2], expr3]].
>
> Yes, I know I could use Throw/Catch here, but on my system that's
> slower, and it would still leave me wondering what the rule is for
> where Return returns to.
>
>



-- 
DrBob at bigfoot.com
www.eclecticdreams.net/index.html


  • Prev by Date: Re: version differences?
  • Next by Date: Re: Re: Re: Re: What is zero divided by zero?
  • Previous by thread: Where does Return return to?
  • Next by thread: Re: Where does Return return to?