From: Helmut J. <jar...@ig...> - 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 |
From: Adam <ada...@o2...> - 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 |
From: Stavros M. <mac...@al...> - 2014-04-27 21:28:31
|
"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 < jar...@ig...> 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 > Max...@li... > https://lists.sourceforge.net/lists/listinfo/maxima-discuss > |
From: Barton W. <wi...@un...> - 2014-04-27 21:50:15
|
For a workaround, see user documentation for "catch" and "throw." |
From: Stavros M. <mac...@al...> - 2014-04-27 22:05:13
|
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 <wi...@un...> 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 > Max...@li... > https://lists.sourceforge.net/lists/listinfo/maxima-discuss > |
From: Robert D. <rob...@gm...> - 2014-04-28 03:36:32
|
On 2014-04-27, Barton Willis <wi...@un...> 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 |