Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

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

Search the Archive

Re: Why Return[] does not work?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg109482] Re: Why Return[] does not work?
  • From: dh <dh at metrohm.com>
  • Date: Thu, 29 Apr 2010 02:53:15 -0400 (EDT)
  • References: <hr6ihp$2f8$1@smc.vnet.net>

On 27.04.2010 13:41, Dims wrote:
> I wrote a function to check if two lists contain equal members:
>
> Intersects[x_List,
>    y_List] := (Do[(Print["Outer step ", xi];
>      Do[(Print[xi, yi];
>        If[xi == yi, (Print["Returning True"]; Return[True])]), {yi,
>        y}]), {xi, x}]; False)
>
> But it returns False for interecting lists:
>
> In[47]:= Intersects[{a, b}, {c, a}]
> During evaluation of In[47]:= Outer step a
> During evaluation of In[47]:= ac
> During evaluation of In[47]:= aa
> During evaluation of In[47]:= Returning True
> During evaluation of In[47]:= Outer step b
> During evaluation of In[47]:= bc
> During evaluation of In[47]:= ba
> Out[47]= False
>
> Few questions:
>
> 1) why does not Return[] interrupt the loops?
> 2) is there better way to implement intersection check?
>
> Thanks
>
Hi,
Return does only return a single level . In your case only the inner Do 
loop is left. Use Throw and Catch to control how many levels to jump up. 
In general, try to avoid Return.
Daniel

-- 

Daniel Huber
Metrohm Ltd.
Oberdorfstr. 68
CH-9100 Herisau
Tel. +41 71 353 8585, Fax +41 71 353 8907
E-Mail:<mailto:dh at metrohm.com>
Internet:<http://www.metrohm.com>



  • Prev by Date: Re: Sphere formula
  • Next by Date: Re: Why Return[] does not work?
  • Previous by thread: Re: Why Return[] does not work?
  • Next by thread: Re: Why Return[] does not work?