## Re: [Maxima-discuss] How to break out of a loop with a block?

 Re: [Maxima-discuss] How to break out of a loop with a block? From: Barton Willis - 2014-04-27 21:50:15 ```For a workaround, see user documentation for "catch" and "throw." ```

 [Maxima-discuss] How to break out of a loop with a block? From: Helmut Jarausch - 2014-04-27 09:49:06 ```The documentation says: Note however that a return within a do that occurs in a block will exit only the do and not the block. The following do does not occur within a block, so why does RetVal:for k in [1,2,3] do block( if k=2 then return(7))\$ print(RetVal)\$ print 'done' instead of 7 ? Thanks for an explanation and a work around, Helmut ```
 Re: [Maxima-discuss] How to break out of a loop with a block? From: Adam - 2014-04-27 19:45:08 ```W dniu 27.04.2014 11:49, Helmut Jarausch pisze: > > The documentation says: > Note however that a return within a do that occurs in a block > will exit only the do and not the block. > > The following do does not occur within a block, so why does > > > RetVal:for k in [1,2,3] do block( > if k=2 then return(7))\$ > > print(RetVal)\$ > > print 'done' instead of 7 ? > > Thanks for an explanation and a work around, > Helmut > ------------------------------------------------------------------------------ > Start Your Social Network Today - Download eXo Platform > Build your Enterprise Intranet with eXo Platform Software > Java Based Open Source Intranet - Social, Extensible, Cloud Ready > Get Started Now And Turn Your Intranet Into A Collaboration Platform > http://p.sf.net/sfu/ExoPlatform > 1. "The value normally returned by a do statement is the atom done. However, the function return may be used inside the body to exit the do prematurely and give it any desired value. Note however that a return within a do that occurs in a block will exit only the do and not the block. Note also that the go function may not be used to exit from a do into a surrounding block. " ( from do doc ) 2. It is not the answer, but result is 7 not done. (%i1) for k in [1,2,3] do if k=2 then RetValue:7 ; (%o1) done (%i2) RetValue; (%o2) 7 HTH Adam http://commons.wikimedia.org/wiki/Category:Images_with_Maxima_CAS_source_code ```
 Re: [Maxima-discuss] How to break out of a loop with a block? From: Stavros Macrakis - 2014-04-27 21:28:31 Attachments: Message as HTML ```"return" exits from the innermost (dynamic) block surrounding it. This may be a 'block' or a 'do'. Note that a function body is *not* a block: block( return(3)) => 3 do return(3) => 3 ​​ ​In your example​, 'return' returns from the block, not from the for/do: ​​ ​for k thru 3 do block( print(k), if k=2 then return(23), print(k*10) ) prints 1,10,2,3,30 and returns 'done'. -s​ ​​​ On Sun, Apr 27, 2014 at 5:49 AM, Helmut Jarausch < jarausch@...> wrote: > > The documentation says: > Note however that a return within a do that occurs in a block > will exit only the do and not the block. > > The following do does not occur within a block, so why does > > > RetVal:for k in [1,2,3] do block( > if k=2 then return(7))\$ > > print(RetVal)\$ > > print 'done' instead of 7 ? > > Thanks for an explanation and a work around, > Helmut > > ------------------------------------------------------------------------------ > Start Your Social Network Today - Download eXo Platform > Build your Enterprise Intranet with eXo Platform Software > Java Based Open Source Intranet - Social, Extensible, Cloud Ready > Get Started Now And Turn Your Intranet Into A Collaboration Platform > http://p.sf.net/sfu/ExoPlatform > _______________________________________________ > Maxima-discuss mailing list > Maxima-discuss@... > https://lists.sourceforge.net/lists/listinfo/maxima-discuss > ```
 Re: [Maxima-discuss] How to break out of a loop with a block? From: Barton Willis - 2014-04-27 21:50:15 ```For a workaround, see user documentation for "catch" and "throw." ```
 Re: [Maxima-discuss] How to break out of a loop with a block? From: Stavros Macrakis - 2014-04-27 22:05:13 Attachments: Message as HTML ```I wouldn't call "catch" and "throw" *workarounds* for "return", which implies that return is not functioning properly. I'd say that they are complementary control operations. -s On Sun, Apr 27, 2014 at 5:50 PM, Barton Willis wrote: > For a workaround, see user documentation for "catch" and "throw." > > > ------------------------------------------------------------------------------ > Start Your Social Network Today - Download eXo Platform > Build your Enterprise Intranet with eXo Platform Software > Java Based Open Source Intranet - Social, Extensible, Cloud Ready > Get Started Now And Turn Your Intranet Into A Collaboration Platform > http://p.sf.net/sfu/ExoPlatform > _______________________________________________ > Maxima-discuss mailing list > Maxima-discuss@... > https://lists.sourceforge.net/lists/listinfo/maxima-discuss > ```
 Re: [Maxima-discuss] How to break out of a loop with a block? From: Robert Dodier - 2014-04-28 03:36:32 ```On 2014-04-27, Barton Willis wrote: > For a workaround, see user documentation for "catch" and "throw." Expanding on this suggestion, how about: foo (j) := catch (for i thru 10 do block (blah, blah, blah, if i=j then throw(i))); foo (5); => 5 foo (20); => done Note that throw/catch is a "nonlocal" mechanism -- 'throw' could be buried within nested blocks or functions calls. best Robert Dodier ```