From: David Bailey on
Dims wrote:
> On 27 =D0=B0=D0=BF=D1=80, 16:28, Albert Retey <a...(a)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[].
>
Yes, but it will exit one Do construct, and you have two!

Everyone needs to use explicit loops sometimes (or at least I do!) but I
find While loops are more convenient:

j=1;
While[j<=10,
do something;
j++
];

One reason is that yo can set a boolean, and add it to the test on the
While - thus exiting as many loops as needed.

I take it you realise that there is a built in function Intersect, and
that this was a programming exercise.

David Bailey
http://www.dbaileyconsultancy.co.uk

From: David Bailey on
Dims wrote:
> On 28 =D0=B0=D0=BF=D1=80, 09:59, "M.Roellig" <markus.roel...(a)googlemail.com>> wrote:
>
>> In[2]:= Intersection[{a, b}, {c, a}] =!= {}
>
> But would Mathematica optimize evaluation of this expression, i.e.,
> terminate searching for common elements if one already found?
>
It would not terminate searching early because the result was being
compared with {}, but it would probably be more efficient anyway because:

a) It would sort the two lists to make the intersection process more
efficient.

b) There is an overhead to each Mathematica step, so that using a
built-in operation is usually more efficient than coding your own - but
if in doubt, you can always check using Timing. It is this overhead that
makes functional operations more efficient, because they just take fewer
steps.

David Bailey
http://www.dbaileyconsultancy.co.uk

From: Fred Simons on
The function Return turns up in this discussion group very regularly.
Already in January, 2002 Allan Hayes posted in this group the following
summary of the behaviour of Return, that has not lost anything of its
importance since then and that answers all questions raised so far:

If Return[x] is generated as a value in Do or Scan then x is immediately
returned;
If Return[x] is generated as an entry in CompoundExpression or as a
value of the body of a While or For loop then Return[x] (not x) is
immediately returned;
If Return[x] is generated as the value of a user-defined function then
the function returns x (not Return[x])
Otherwise Return[x] behaves as a ordinary expression.

Allan did not include the use of Return to end a Dialog session. In that
case, Return should be used as a stand alone command, not e.g. in a
compound expression or in a button.

My feeling is that the intended purpose of the function Return is to
exit from a user-defined function with the argument of Return as the
value of the function call. That is a relict from programming languages
a long time ago, when indeed it was necessary to use this command to get
a value out. There is no reason to use this command in this way
nowadays. But it explains e.g. why the result of a compound expression
with a Return argument has to be a Return expression. In the following
artificial example we have two nested compound expressions:

In[63]:= f[___] := Module[{y=0}, y=y+1; If[y>0,
y=y+1;Return[y];y=y+10];y=y+100]
f[]

Out[64]= 2

During the evaluation of the inner compound expression we arrive at
Return[y], so we want to have that value of y (that is 2) as the result
of the function call. If y was not wrapped in Return, the outer compound
expression would be completely evaluated, resulting in the value 102
instead of 2.

For the same reason the result of While and For with a Return is wrapped
in Return. The fact that Do and Scan behave differently does not seem
very very consequent. Further, Return has no effect at all when it turns
up in the result of a Table or a Map command, two other loop constructs.

I think that Return is not intended for interrupting loops. The
documentation states that Return[expr] returns the value expr from a
function. Not a very clear description, but in line with the above
remarks. As many have already said, for interrupting loops the
Catch-Throw construction is much more appropriate.

Finally two more examples of 'strange' behaviour:

By defining a function with Blank's, we create a user defined rule, and
Allan's survey also holds for user defined rules. Consider the immediate
assignment a=Return[3]. Evaluation of the right hand side of course
returns Return[3]. But since we have created a rule for the symbol a
(see OwnValues[a]), calling a gives 3 instead of Return[3].

A pure function does not create a user defined rule, so in pure
functions Return will be persistent:

In[26]:= f[x_] := Return[x];
g=Return[#]&;
{f[a], g[a]}

Out[28]= {a,Return[a]}

Fred Simons
Eindhoven University of Technology

From: Andrzej Kozlowski on
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...(a)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[].
>


From: image_doctor on
> 2) is there better way to implement intersection check?


You could try this relatively elegant solution ....

IntersectQ[a_, a_] := True
IntersectQ[a_, b_] := False


With the following timing results.

n=100000000;
r1 = RandomInteger[{1,5},{n}];
r2 = RandomInteger[{1,5},{n}];
r3=r1;

Timing[IntersectQ[r1,r1]]
Timing[IntersectQ[r1,r2]]
Timing[IntersectQ[r1,r3]]
Out[55]= {0.,True}
Out[56]= {0.,False}
Out[57]= {0.,True}


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