Re: Why Return[] does not work?
- To: mathgroup at smc.vnet.net
- Subject: [mg109509] Re: Why Return[] does not work?
- From: David Bailey <dave at removedbailey.co.uk>
- Date: Sun, 2 May 2010 05:36:18 -0400 (EDT)
- References: <hr6ihp$2f8$1@smc.vnet.net> <hr6la2$5im$1@smc.vnet.net> <201004280558.BAA05169@smc.vnet.net> <hre94j$8s$1@smc.vnet.net>
Andrzej Kozlowski wrote:
> I am not sure if anyone pointed out that if enough Returns are used the original code will work as expected:
>
> Intersects[x_List, y_List] := (Do[(Print["Outer step ", xi];
> Do[(Print[xi, yi];
> If[xi == yi, (Print["Returning True"];
> Return[Return[Return[True]]])]), {yi, y}]), {xi, x}]; False)
>
> In[2]:= Intersects[{a,b},{c,a}]
> During evaluation of In[2]:= Outer step a
> During evaluation of In[2]:= ac
> During evaluation of In[2]:= aa
> During evaluation of In[2]:= Returning True
> Out[2]= True
>
> On 28 Apr 2010, at 14:58, Dims wrote:
>
>> On 27 =D0=B0=D0=BF=D1=80, 16:28, Albert Retey <a... at gmx-topmail.de> wrote:
>>> Am 27.04.2010 13:41, schrieb Dims:
>>> Intersects[x_List, y_List] := Catch[
>> Yes, thank you, I also came to conclusion, that catch works. But I
>> think it is a bug with return, since manual says, that we CAN use
>> Return[] inside Do[].
>>
>
>
With Mathematica you always live and learn - I had never realised that
would work - thanks!
I do use Return sometimes, but if I need a loop, I normally use a While
loop - which don't seem to be noticed by Return. Unlike some others, I
think Return has its place, although I do wish its semantics were a
little clearer.
If you want a function to perform a few tests on its arguments to decide
if its arguments are appropriate, I think it is much clearer to use
If[xxx,Return[]]; rather than embed the rest of the function body inside
the If.
I also think Return is useful when making temporary alterations to some
code - say adding a Return[] somewhere just to disable some code.
David Bailey
http://www.dbaileyconsultancy.co.uk