Skip to main content

programming - What can I use as the second argument to Return in my own functions?


Having found out about the second argument to Return I played a bit with it, and the following tests all resulted in Return::nofunc:


f[x_]:=x
f[Return[3,f]]

SetAttributes[g,HoldAll];g[x_]:=x
g[Return[3,g]]


h[x_]:=f[x]
h[Return[3,f]]

SetAttributes[i,HoldAll];i[x_]:=f[x]
i[Return[3,f]]

On the other hand, the following works fine:


Module[{i},Return[3,Module]]

Does that mean the second argument of Return can only be used with a predefined list of symbols? If so, is there an easy way to find out that list? Otherwise, what am I doing wrong (and how would I do it right)?




Answer



Ok, my view on this is consistent with all of the cases presented so far...


The symbols allowed in Return are those which are responsible for a transformation in the partial evaluation of the expression so far. Of course, I'm referring only to the evaluation of the branch that includes the Return. There's a subtlety but I'll mention it later


Let's clarify this with your examples:


f[x_]:=x
f[Return[3,f]]

f has no attributes so, Return is run before any transformation is made. Doesn't work


h[x_]:=f[x]
h[Return[3,f]]


Again, Return[3, f] is executed first.


SetAttributes[i,HoldAll];
i[x_]:=f[x]
i[Return[3,f]]

Here, i is HoldAll so now we start talking. The i[Return[3,f]]->f[Return[3, f]] is first made. i was the symbol responsible. Now, we run f[Return[3, f]], but f has no attributes, so the Return[3, f] is run, and again, can't find f. Could have found i however.


Last one (actually second on your list, but I thought I'd leave it for last). Here comes the subtlety: Actually, not all symbols responsible for transformations are available. When there's a transformation associated with a symbol, say, s1, and then there's another one AT THE SAME LEVEL, associated with the symbol s2, then s1 is shadowed by s2 and no longer available


SetAttributes[g,HoldAll];
g[x_]:=x

g[Return[3,g]]

g is HoldAll, so that's a good start. First step: g[Return[3, g]]->Return[3, g] and g is responsible for the transformation... So, it would seem that we are succeeding. HOWEVER, the very last transformation from Return[3, g] to whatever, makes the symbol Return shadow g. So, this could be fixed by just making the Return work at a lower level


SetAttributes[g,HoldAll];
g[x_]:=# &[x]
g[Return[3,g]]

It is interesting to note that Return[2, Return] works, and would have worked in the previous example too.


Ok, now let's go to the cases where it actually works


Module[{i},Return[3,Module]]


Module is HoldAll. First, there's a transformation associated to Module. If that transformation returned Return[3, Module], then this wouldn't work. But this works. So, I choose to believe that despite what Trace shows, Module evaluated the Return expression internally before returning, just like our g[x_]:=# &[x] example.


@belisarius on the comments:


f[x_] := g[Return[x, f]];
f[3]

f[3]->g[Return[3, f]]. f was responsible. Now, Return[3, f] is evaluated (at a lower level in the expression tree) and success.


Bonus example:


SetAttributes[f, HoldAll];
g /: f[i_, g] := {i}


Now


f[Return[2, g], g]

This actually works! Why?


f[Return[2,g], g] is turned into {Return[2, g]} BECAUSE OF g. Now, Return is evaluated (at a lower level) and that's the way the cookie crumbles


Summary


Imagine the expressions as trees, and the evaluation procedure as a succession of trees, and in each transformation, draw a sign pointing at the new sub-tree, with the symbol responsible written on it. If you ever have to transform that same subtree again, the previous sign is lost. When it's time for Return to evaluate, you go up the tree and see the signs in the path to the top. Those are the ones you can use.


Comments

Popular posts from this blog

front end - keyboard shortcut to invoke Insert new matrix

I frequently need to type in some matrices, and the menu command Insert > Table/Matrix > New... allows matrices with lines drawn between columns and rows, which is very helpful. I would like to make a keyboard shortcut for it, but cannot find the relevant frontend token command (4209405) for it. Since the FullForm[] and InputForm[] of matrices with lines drawn between rows and columns is the same as those without lines, it's hard to do this via 3rd party system-wide text expanders (e.g. autohotkey or atext on mac). How does one assign a keyboard shortcut for the menu item Insert > Table/Matrix > New... , preferably using only mathematica? Thanks! Answer In the MenuSetup.tr (for linux located in the $InstallationDirectory/SystemFiles/FrontEnd/TextResources/X/ directory), I changed the line MenuItem["&New...", "CreateGridBoxDialog"] to read MenuItem["&New...", "CreateGridBoxDialog", MenuKey["m", Modifiers-...

How to thread a list

I have data in format data = {{a1, a2}, {b1, b2}, {c1, c2}, {d1, d2}} Tableform: I want to thread it to : tdata = {{{a1, b1}, {a2, b2}}, {{a1, c1}, {a2, c2}}, {{a1, d1}, {a2, d2}}} Tableform: And I would like to do better then pseudofunction[n_] := Transpose[{data2[[1]], data2[[n]]}]; SetAttributes[pseudofunction, Listable]; Range[2, 4] // pseudofunction Here is my benchmark data, where data3 is normal sample of real data. data3 = Drop[ExcelWorkBook[[Column1 ;; Column4]], None, 1]; data2 = {a #, b #, c #, d #} & /@ Range[1, 10^5]; data = RandomReal[{0, 1}, {10^6, 4}]; Here is my benchmark code kptnw[list_] := Transpose[{Table[First@#, {Length@# - 1}], Rest@#}, {3, 1, 2}] &@list kptnw2[list_] := Transpose[{ConstantArray[First@#, Length@# - 1], Rest@#}, {3, 1, 2}] &@list OleksandrR[list_] := Flatten[Outer[List, List@First[list], Rest[list], 1], {{2}, {1, 4}}] paradox2[list_] := Partition[Riffle[list[[1]], #], 2] & /@ Drop[list, 1] RM[list_] := FoldList[Transpose[{First@li...

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...