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

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[ ...

equation solving - Invert and fit implicitly defined curve

I need to fit an implicitly defined curve. I thought I could get some data out of Solve , and then using FindFit . Therefore, I would like to find the relation the parametric curve defined by $F(x,y)=0$: Solve[-(1/2) + 1/2 (0.41202 BesselK[0, 0.1 Sqrt[x^2 + y^2]] + (0.101483 x BesselK[1, 0.1 Sqrt[x^2 + y^2]])/Sqrt[x^2 + y^2]) == 0, y] But I can't get an output: Solve was unable to solve the system with inexact coefficients or the system obtained by direct rationalization of inexact numbers present in the system. Since many of the methods used by Solve require exact input, providing Solve with an exact version of the system may help. >> Edit: In particular, I would like to fit the data coming from the curve with the expression of another curve, and not with a function $f(x)$. In particular, since this clearly looks like a cardioid , I would like it to fit to something like it. What other strategies could I try?

dynamic - How can I make a clickable ArrayPlot that returns input?

I would like to create a dynamic ArrayPlot so that the rectangles, when clicked, provide the input. Can I use ArrayPlot for this? Or is there something else I should have to use? Answer ArrayPlot is much more than just a simple array like Grid : it represents a ranged 2D dataset, and its visualization can be finetuned by options like DataReversed and DataRange . These features make it quite complicated to reproduce the same layout and order with Grid . Here I offer AnnotatedArrayPlot which comes in handy when your dataset is more than just a flat 2D array. The dynamic interface allows highlighting individual cells and possibly interacting with them. AnnotatedArrayPlot works the same way as ArrayPlot and accepts the same options plus Enabled , HighlightCoordinates , HighlightStyle and HighlightElementFunction . data = {{Missing["HasSomeMoreData"], GrayLevel[ 1], {RGBColor[0, 1, 1], RGBColor[0, 0, 1], GrayLevel[1]}, RGBColor[0, 1, 0]}, {GrayLevel[0], GrayLevel...