Re: Return in function
- To: mathgroup at smc.vnet.net
- Subject: [mg105710] Re: Return in function
- From: Raffy <raffy at mac.com>
- Date: Wed, 16 Dec 2009 06:18:37 -0500 (EST)
- References: <hftbe9$cjh$1@smc.vnet.net>
On Dec 11, 3:46 am, dh <d... at metrohm.com> wrote: > Version 7.0.1 > > Hello, > > I am not sure if this is a feature or a bug. If you use Return in an > > anonymous function, not only the return value is returned, but also > > "Return" itself. Consider: > > (If[# == 2, Return[a]; #, #]) & /@ {1, 2, 3} > > this gives: > > {1, Return[a], 3} > > The same thing happens with: > > Function[x, If[x == 2, Return[a]; x, x]] /@ {1, 2, 3} > > However, the following works as expected: > > f[x_] := (If[x == 2, Return[a]; x, x]); > > f /@ {1, 2, 3} > > Daniel I completely agree with the Return[]-retardness. Proper Return[]- behavior can be implemented using Catch/Throw, but the overhead is atrocious. To the naysayers: Mathematica is multi-paradigm; I see nothing wrong with procedural code if it makes your code more readable and maintainable. I feel the most elegant improvement for future versions of Mathematica would BlockReturn[], WithReturn[], and ModuleReturn[]. However, this would still leave naked Return[]'s in their current state.