MathGroup Archive 2005

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

Search the Archive

Re: Re: Do loops in Mathematica

  • To: mathgroup at smc.vnet.net
  • Subject: [mg55287] Re: [mg55174] Re: Do loops in Mathematica
  • From: DrBob <drbob at bigfoot.com>
  • Date: Fri, 18 Mar 2005 05:34:11 -0500 (EST)
  • References: <d15s6o$9l2$1@smc.vnet.net> <200503161035.FAA23736@smc.vnet.net> <opsnqpbopgiz9bcq@monster.ma.dl.cox.net> <4239258B.9090200@metrohm.ch>
  • Reply-to: drbob at bigfoot.com
  • Sender: owner-wri-mathgroup at wolfram.com

One of the many reasons I never use Return is that you can't be sure where it will return FROM. It's probably not returning at all, in these examples:

Do[{Return[1]},{i,1,1}]

Do[Print@{Return[1]},{i,1,1}]

{Return[1]}

Do[Print@Return[1],{i,1,1}]

Return[1]

In all three cases, the value of Do[...] should be 1 if Return exits Do, so it clearly doesn't. The first printed result should be 1 if Return exited List, no print at all if it exited from Print or Do. The last printed result shouldn't exist if Return exited Print or Do, and there's nothing else for it to exit.

This adheres to documentation (arguably) if we suppose List and Print are not loops or procedures, since Documentation says Return will "return the value expr, exiting all procedures and loops in a function". Does anyone know the official definition of "loop" or "procedure" or "control structure"? I certainly don't.

Anyway, don't use Return, and this will never affect you.

Throw and Catch are far more reliable.

Bobby

On Thu, 17 Mar 2005 07:36:59 +0100, Daniel Huber <dh at metrohm.ch> wrote:

> Hi Bob,
> sure your are right, x2 was a typo.
> However, there is something more serious.
> Please explain to me why the following should be considered a feature
> and not a bug:
>
> Do[Return[1],{i,1,1}]     does return 1
> Do[{Return[1]},{i,1,1}] does return Null
>
> Daniel
>
> DrBob wrote:
>
>> There is no bug, and...
>>
>> This really works?
>>
>> Do[p = Solve[x2 + 3*x + 1 == 0,
>>      x]; Return[2*p], {i, 1}]
>>
>> {{2*(x -> (1/3)*(-1 - x2))}}
>>
>
>



-- 
DrBob at bigfoot.com


  • Prev by Date: Re: Re: Sum
  • Next by Date: Re: How can I compute the Fourier transform of a unit disk and a unit ball analytically by using Mathematica?
  • Previous by thread: Re: Re: Do loops in Mathematica
  • Next by thread: Re: Do loops in Mathematica