From: Dims on
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

From: Albert Retey on
Am 27.04.2010 13:41, schrieb Dims:
> 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?

Return behaves not as people expect in many circumstances. I found that
I almost never really need it in my code because for every possible use
case there are other mechanisms that work more reliable.

> 2) is there better way to implement intersection check?

There is Break which would be the natural choice to break loops,
but I think usually Throw and Catch are the best choice in these situations:

Intersects[x_List, y_List] := Catch[
Do[
Print["Outer step ", xi];
Do[
Print[xi, yi];
If[xi == yi, Print["Returning True"]; Throw[True]],
{yi, y}
], {xi, x}
];
False
]

depending on what you try to achieve there might be better mechanisms
than do loops which stop at certain conditions...

hth,

albert

From: DrMajorBob on
IMHO, Return doesn't work because it's a bad idea. Don't use it.

Bobby

On Tue, 27 Apr 2010 06:41:50 -0500, Dims <dims12(a)hotmail.com> 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
>


--
DrMajorBob(a)yahoo.com

From: dh on
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(a)metrohm.com>
Internet:<http://www.metrohm.com>


From: Raffy on
On Apr 27, 5:28 am, Albert Retey <a...(a)gmx-topmail.de> wrote:
> Am 27.04.2010 13:41, schrieb Dims:
>
>
>
>
>
> > 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?
>
> Return behaves not as people expect in many circumstances. I found that
> I almost never really need it in my code because for every possible use
> case there are other mechanisms that work more reliable.
>
> > 2) is there better way to implement intersection check?
>
> There is Break which would be the natural choice to break loops,
> but I think usually Throw and Catch are the best choice in these situatio=
ns:
>
> Intersects[x_List, y_List] := Catch[
> Do[
> Print["Outer step ", xi];
> Do[
> Print[xi, yi];
> If[xi == yi, Print["Returning True"]; Throw[True]],
> {yi, y}
> ], {xi, x}
> ];
> False
> ]
>
> depending on what you try to achieve there might be better mechanisms
> than do loops which stop at certain conditions...
>
> hth,
>
> albert

Catch/Throw however cause overhead.

If you're return is just a boolean, and your control structure depth
is just 1, you could exploit the fact that: Do[Return[x],{1}] === x
and Do[Null,{1}] === Null.

intersects[x_List, y_List] := Do[If[xi == yi, Return[True]], {yi, y},
{xi, x}] === True;

 |  Next  |  Last
Pages: 1 2 3 4
Prev: InputField Rounding Problem
Next: does not work?