Skip to main content
Tweeted twitter.com/StackMma/status/1171302403783766016
edited tags
Link
Alexey Popkov
  • 62.3k
  • 7
  • 154
  • 375
deleted 38 characters in body; edited tags; edited title
Source Link
rm -rf
  • 89.3k
  • 21
  • 297
  • 479

How does Return[]Return work?

It is not completely clear to me how Return[] works.

The documentation says:

Return[expr] returns the value expr from a function.

But in MathematicaMathematica it is not clear where are the boundaries of a "function" defined using patterns and Set(Delayed). The system simply applies replacement rules until there's nothing to change anymore.

Consider these examples:

In[1]:= a := (Module[{}, Return[0]; 1]; 2)

In[2]:=     
a
Out[2]=(* 0 *)

It is not completely clear to me why Return breaks out from a, and not from Module or one of the CompoundExpression.

The following doesn't work, and simply returns the Return expression. It reminds me a bit of how Unevaluated is handled.

In[3]:= (Module[{}, Return[0]; 1]; 2)
Out[3]=(* Return[0] *)

If we add another layer of definitions, the effect of Return will stop there:

In[4]:= b := (a; 3)
Out[4]=(* 3 *)

Why do these three inputs give different results? What is the general rule for deciding where Return breaks out to precisely?

The answer lies somewhere in fully understanding the evaluation process.

Would someone care to elucidate this point, perhaps with pointers to the parts of the documentation which make this clear?

How does Return[] work?

It is not completely clear to me how Return[] works.

The documentation says:

Return[expr] returns the value expr from a function.

But in Mathematica it is not clear where are the boundaries of a "function" defined using patterns and Set(Delayed). The system simply applies replacement rules until there's nothing to change anymore.

Consider these examples:

In[1]:= a := (Module[{}, Return[0]; 1]; 2)

In[2]:= a
Out[2]= 0

It is not completely clear to me why Return breaks out from a, and not from Module or one of the CompoundExpression.

The following doesn't work, and simply returns the Return expression. It reminds me a bit of how Unevaluated is handled.

In[3]:= (Module[{}, Return[0]; 1]; 2)
Out[3]= Return[0]

If we add another layer of definitions, the effect of Return will stop there:

In[4]:= b := (a; 3)
Out[4]= 3

Why do these three inputs give different results? What is the general rule for deciding where Return breaks out to precisely?

The answer lies somewhere in fully understanding the evaluation process.

Would someone care to elucidate this point, perhaps with pointers to the parts of the documentation which make this clear?

How does Return work?

It is not completely clear to me how Return[] works.

The documentation says:

Return[expr] returns the value expr from a function.

But in Mathematica it is not clear where are the boundaries of a "function" defined using patterns and Set(Delayed). The system simply applies replacement rules until there's nothing to change anymore.

Consider these examples:

a := (Module[{}, Return[0]; 1]; 2)     
a
(* 0 *)

It is not completely clear to me why Return breaks out from a, and not from Module or one of the CompoundExpression.

The following doesn't work, and simply returns the Return expression. It reminds me a bit of how Unevaluated is handled.

(Module[{}, Return[0]; 1]; 2)
(* Return[0] *)

If we add another layer of definitions, the effect of Return will stop there:

b := (a; 3)
(* 3 *)

Why do these three inputs give different results? What is the general rule for deciding where Return breaks out to precisely?

The answer lies somewhere in fully understanding the evaluation process.

Would someone care to elucidate this point, perhaps with pointers to the parts of the documentation which make this clear?

Post Migrated Here from stackoverflow.com (revisions)
Source Link
Szabolcs
  • 236.5k
  • 31
  • 641
  • 1.3k

How does Return[] work?

It is not completely clear to me how Return[] works.

The documentation says:

Return[expr] returns the value expr from a function.

But in Mathematica it is not clear where are the boundaries of a "function" defined using patterns and Set(Delayed). The system simply applies replacement rules until there's nothing to change anymore.

Consider these examples:

In[1]:= a := (Module[{}, Return[0]; 1]; 2)

In[2]:= a
Out[2]= 0

It is not completely clear to me why Return breaks out from a, and not from Module or one of the CompoundExpression.

The following doesn't work, and simply returns the Return expression. It reminds me a bit of how Unevaluated is handled.

In[3]:= (Module[{}, Return[0]; 1]; 2)
Out[3]= Return[0]

If we add another layer of definitions, the effect of Return will stop there:

In[4]:= b := (a; 3)
Out[4]= 3

Why do these three inputs give different results? What is the general rule for deciding where Return breaks out to precisely?

The answer lies somewhere in fully understanding the evaluation process.

Would someone care to elucidate this point, perhaps with pointers to the parts of the documentation which make this clear?