Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!feed.xsnews.nl!border-1.ams.xsnews.nl!newspump.sol.net!posts.news.twtelecom.net!nnrp3.twtelecom.net!not-for-mail From: "Oleksandr Rasputinov" Newsgroups: comp.soft-sys.math.mathematica Subject: Re: NMinimize problem: fct minimized uses FindRoot Date: Wed, 14 Dec 2011 11:09:35 +0000 (UTC) Organization: Steven M. Christensen and Associates, Inc and MathTensor, Inc. Sender: steve@smc.vnet.net Approved: Steven M. Christensen , Moderator Message-ID: References: <201112101227.HAA19219@smc.vnet.net> Lines: 67 NNTP-Posting-Date: 14 Dec 2011 10:14:18 GMT NNTP-Posting-Host: cb295ad7.news.twtelecom.net X-Trace: DXC=0@XYBTh>P0^mmJjon<[c^_C_A=>8kQj6]=_1NR_H?JP];T8l36j_JW_EFiONJ7[GoVXM2<;IL@h2Q X-Complaints-To: abuse@twtelecom.net Xref: x330-a1.tempe.blueboxinc.net comp.soft-sys.math.mathematica:7205 On Tue, 13 Dec 2011 10:41:11 -0000, Andrzej Kozlowski wrote: > > On 12 Dec 2011, at 12:43, Oleksandr Rasputinov wrote: > >> For example, the erroneous usage: >> >> Block[{a = 1, b = 2}, Return[a]; Return[b]] >> >> gives: >> >> Return[1] >> >> but a correct usage: >> >> Do[Block[{a = 1, b = 2}, Return[a]; Return[b]], {1}] >> >> gives (because of the enclosing Do): >> >> 1 > > This is not quite the whole story. For example, while the above use of > Return in Block does not work, this one (in a function call) does: > > f[] := Block[{a = 1, b = 2}, Return[a]; Return[b]] > > f[] > > 1 > > Moreover, if you replace the Do loop in your example with a While loop, > you will get: > > n = 1; While[n < 10, Block[{a = 1, b = 2}, Return[a]; Return[b]]] > > Return[1] > > All of this is purposeful design and can be explained and justified but > I don't think it's worth the bother since Mathematica has far superior > means of flow control than this clumsy and arcane construct. > > Andrzej > Thanks for the additional examples. Yes, there are limitations to what Return (or Break, or Continue) will accept as a control flow construct; I imagine most people would have suspected that Block does not count but it is a little surprising to find that While doesn't either. The documentation for Return also states that "Return[expr] exits control structures within the definition of a function, and gives the value expr for the whole function", by which it apparently means that delayed definitions do count as enclosing control flow, although in apparent contradiction to the documented behaviour Function does not. (This seems to be another example of a virtually meaningless statement about functions in the documentation made on the assumption that the reader is stuck in the mindset of another language and prefers a loose analogy to the actual behaviour over a factually accurate account.) Oliver Ruebenkoenig's reply demonstrates the almost undocumented two-argument form of Return, the existence of which (I think) suggests that the list of constructs accepted by Return were not considered completely indisputable by the designers of Mathematica. However, the two-argument form still cannot be used inside Compile, where it seems it would be most useful.