Re: Return in function
- To: mathgroup at smc.vnet.net
- Subject: [mg105636] Re: [mg105621] Return in function
- From: DrMajorBob <btreat1 at austin.rr.com>
- Date: Sat, 12 Dec 2009 06:57:17 -0500 (EST)
- References: <200912111146.GAA12933@smc.vnet.net>
- Reply-to: drmajorbob at yahoo.com
Return doesn't work as documented, but that's not the best reason to never
use it.
Your three examples should have been written as
If[# == 2, a, #] & /@ {1, 2, 3}
{1, a, 3}
Function[x, If[x == 2, a, x]] /@ {1, 2, 3}
{1, a, 3}
f[x_] := If[x == 2, a, x]
f /@ {1, 2, 3}
{1, a, 3}
Tortured constructions give unpredictable results, as a matter of simple
justice.
Bobby
On Fri, 11 Dec 2009 05:46:49 -0600, dh <dh 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
>
>
>
--
DrMajorBob at yahoo.com
- References:
- Return in function
- From: dh <dh@metrohm.com>
- Return in function