MathGroup Archive 2009

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

Search the Archive

Re: Return Ambiguity

  • To: mathgroup at smc.vnet.net
  • Subject: [mg97569] Re: Return Ambiguity
  • From: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
  • Date: Sun, 15 Mar 2009 05:30:19 -0500 (EST)
  • References: <gpaotd$qeb$1@smc.vnet.net> <gpdacm$jrb$1@smc.vnet.net>

Doc page quote: "Return[expr] exits control structures within the
definition of a function, and gives the value expr for the whole
function. "

Most of the exaples you provide here show the use of Return not in the
DEFINITION of a fuction. So, clearly not the intended use. Put them in
a function definition and they work fine and as expected. See below:


In[60]:= a := (Return[#]; 1) &[2]
a
a := Block[{}, Return[1]; 2]
a
a := If[True, Return[1], 2]
a
a := (Do[Return[1], {1}]; 2)
a
a := (Block[{}, Return[1]]; 2)
a
a := Do[Return[Evaluate[Return[1]]], {1}]
a
f := (Module[{}, If[True, Return[1]]]; 2); f

a := 1 /. {1 :> (Return[1]; 2)}
a

Out[61]= 2

Out[63]= 1

Out[65]= 1

Out[67]= 2

Out[69]= 1

Out[71]= 1

Out[72]= 1

Out[74]= 1

Cheers -- Sjoerd

On Mar 14, 12:42 pm, Raffy <ra... at mac.com> wrote:
> No ambiguity? :p
>
> What do these return?
>
> (Return[#]; 1) &[2]
>
> Block[{}, Return[1]; 2]
>
> If[True, Return[1]; 2]
>
> (Do[Return[1], {1}]; 2)
>
> (Block[{}, Return[1]]; 2)
>
> Do[Return[Evaluate[Return[1]]], {1}]
>
> f := (Module[{}, If[True, Return[1]]]; 2); f
>
> 1 /. {1 :> (Return[1]; 2)}
>
> It seems like the documentation should say:
>
> 1. Properly exits to nearest Do[...]-instruction or SetDelayed-
> evaluation.
> 2. Exits all other evaluations oddly by wrapping result in head
> Return, including a pure function.
>
> I feel that Catch/Throw introduces significant speed overhead.
> A simple return shouldn't need to invoke pattern matching.
>
> Usually, the use of Return can be avoided, however, often the
> equivalent Return-less code usually has excessive indenting.
>
> I'd say a proper replacement, would be a function that returns to
> nearest scoping construct, ie. Block/Module/With.



  • Prev by Date: Re: Weird NMinimize behaviour
  • Next by Date: Re: Comparing Corresponding Columns of Two Matrices
  • Previous by thread: Re: Re: Return Ambiguity
  • Next by thread: max. value in a plot?