MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Return in function



-----Original Message-----
From: David Bailey [mailto:dave at removedbailey.co.uk] 
Sent: Sunday, December 13, 2009 11:38 AM
To: mathgroup at smc.vnet.net
Subject: [mg105665] [mg105641] Re: Return in function

DrMajorBob wrote:
> 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
> 

Aren't you being a bit unfair, Daniel has obviously taken time to thin 
out this example from whatever complicated context he found it in 
originally - minimal examples of problems often don't make sense:)

David Bailey
http://www.dbaileyconsultancy.co.uk

Hi,

Sorry to intervene in the thread.

In any case, I am unable to grasp the kind of transgression made by Daniel.
As seen from "outside", Function[x, If[x == 2, Return[a], x]] /@ {1, 2, 3}
should work as expected. The problem is not with Daniel but with the so
called "unexpected results". I do not see any "tortured construction" in the
expression "Function[x, If[x == 2, Return[a], x]] /@ {1, 2, 3}". Is it
syntactically incorrect? No, it is not. And, if it is not, then the
compiler/interpreter should render what it is being asked to.

It sounds as if the semantics of the "Function[]" or "If[]" constructs were
a bit tortured instead. See this example from the Mathematica Help for
Return[] (Version 6). Strictly equivalent syntax form, but semantically
different according to the results 

In[1]:= f1[x_] := (If[x > 5, Return[x]]; x + 3)

In[2]:= f1[6]

Out[2]= 6

Or the equivalent

In[1]:= f1[x_] := (If[x > 5, Return[a]]; x + 3)

In[2]:= f1[6]

Out[2]= a

And now the same expression under the Function[] construct

In[3]:= f2 = Function[{x}, (If[x > 5, Return[x]]; x + 3)];

In[4]:= f2[6]

Out[4]= Return[6]

Why does Return[] behave so different inside the Function[] construct. Maybe
nothing is wrong but clearly something is tricky.


E. Martin-Serrano 



  • Prev by Date: Re: Curve-fitting,
  • Next by Date: question
  • Previous by thread: Re: Return in function
  • Next by thread: Re: Re: Return in function