Here is what I tried to do:
Scan[Function[x,
Scan[Function[y,
If[y == 2, Return[]];
(* Do something useful *) Print[y]],
x]],
{{1, 2, 3}, {4, 5, 6}}]
My intention was that the Return[] exited from the innermost enclosing pure function (having the parameter y). Instead, it terminated execution of the innermost enclosing Scan and proceeded to the next iteration of the outermost Scan.
1 4 5 6
The documentation for Return says:
Returnexits only the innermost construct in which it is invoked
I do not see why the innermost Scan is considered the innermost enclosing construct here. If, CompoundExpression and Function look like more inner constructs.
Question 1: Could you give a more clear explanation of what precisely Return does?
After I realized that Return does not do what I want here, I looked for workaround, and found several ones:
(a) I can restructure the body of the innermost pure function:
Scan[Function[x,
Scan[Function[y,
If[y != 2,
(* Do something useful *) Print[y]]
],
x]],
{{1, 2, 3}, {4, 5, 6}}]
In this case it looks straighforward, but becomes messy if I have more nested control flow operators and multiple points where I might need to exit.
(b) I can use infamous Label and Goto constructs:
Scan[Function[x,
Scan[Function[y,
Module[{exit},
If[y == 2, Goto[exit]];
(* Do something useful *) Print[y];
Label[exit];
]],
x]],
{{1, 2, 3}, {4, 5, 6}}]
(c) I can use Throw and Catch constructs:
Scan[Function[x,
Scan[Function[y,
Module[{exit},
Catch[
If[y == 2, Throw[Null, exit]];
(* Do something useful *) Print[y],
exit]]],
x]],
{{1, 2, 3}, {4, 5, 6}}]
Question 2: Which of them would you prefer? Or can you suggest a better way to do this?
Returnbut it is here:Return::nofunc– Chris Degnen Dec 05 '13 at 17:11