From: andre m. <and...@gm...> - 2019-05-02 20:46:02
|
looks like I'm going to write a small shell script with some added sed code around my maxima invocations Thanks Andre On 5/2/19 9:13 PM, Stavros Macrakis (Σταῦρος Μακράκης) wrote: > As Barton said, we already have catch/throw. You can write: > > myfunc(...):=catch( ... > ... throw(result) ...) > > If you really want, you could easily define a macro tblock (throw-block) > which is equivalent to catch(block(...)). > > The main problem with all of Maxima's throw and return mechanisms is that > there is no way to return to anything but the *innermost* catch or block. > > -s > > On Thu, May 2, 2019 at 2:09 PM andre maute <and...@gm...> wrote: > >> So my next logical question would be, >> if it is possible to extend Maxima >> with an extra C/C++-like return function? >> >> Let's call it 'pt_c_return'. >> >> So if I want to prototype something for C++, i would simply use >> 'pt_c_return' instead of 'return' and be happy. >> >> Perhaps a 'pt_c_block' instead of 'block' could be necessary, too. >> >> Regards >> Andre >> >> On 5/2/19 1:54 AM, Barton Willis wrote: >>> -- Function: return (<value>) >>> May be used to exit explicitly from the current 'block', 'while', >>> 'for' or 'do' loop bringing its argument. It therefore can be >>> compared with the 'return' statement found in other programming >>> languages but it yields one difference: In maxima only returns from >>> the current block, not from the entire function it was called in. >>> In this aspect it more closely resembles the 'break' statement from >>> C. >>> >>> >>> -- Function: catch (<expr_1>, ..., <expr_n>) >>> Evaluates <expr_1>, ..., <expr_n> one by one; if any leads to the >>> evaluation of an expression of the form 'throw (arg)', then the >>> value of the 'catch' is the value of 'throw (arg)', and no further >>> expressions are evaluated. This "non-local return" thus goes >>> through any depth of nesting to the nearest enclosing 'catch'. If >>> there is no 'catch' enclosing a 'throw', an error message is >>> printed. >>> >>> >>> >>> --Barton >>> >>> >>> ________________________________ >>> From: andre maute <and...@gm...> >>> Sent: Wednesday, May 1, 2019 17:55 >>> To: <max...@li...> >>> Subject: [Maxima-discuss] using return within a for loop within a >> function >>> >>> Hi list, >>> >>> this one baffles me most of the times when prototyping >>> mathematical code in Maxima respectively C++ >>> and converting between the two languages. >>> >>> We have the following two snippets, where the >>> first one outputs "1" and the second outputs "-1" >>> >>> Do I miss something here? >>> I often resort in these situations to >>> a solution with extra flags in the Maxima code. >>> >>> Regards >>> Andre >>> >>> >>> ---testreturn.mac--- >>> display2d : false; >>> >>> my_foo(d1,d2) := block( >>> >>> [res:-1,m], >>> >>> for m : 1 thru d2 do block( >>> if (m = d1) then block( >>> return(res) >>> ) >>> ), >>> >>> return(d1) >>> ); >>> >>> print( my_foo(1,10) ); >>> >>> ---testreturn.mac--- >>> >>> ---testreturn.cpp--- >>> #include <iostream> >>> >>> int foo(int d1, int d2) >>> { >>> int res = -1; >>> >>> for(int m = 1; m <= d2; m++) { >>> if(m == d1) { >>> return(res); >>> } >>> } >>> >>> return(d1); >>> } >>> >>> int main() >>> { >>> std::cout << foo(1,10) << std::endl; >>> return 0; >>> } >>> ---testreturn.cpp--- >>> >>> >>> _______________________________________________ >>> Maxima-discuss mailing list >>> Max...@li... >>> >> https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.sourceforge.net_lists_listinfo_maxima-2Ddiscuss&d=DwICAg&c=Cu5g146wZdoqVuKpTNsYHeFX_rg6kWhlkLF8Eft-wwo&r=Ln0CjFotuA7GyhsRS-QpQA&m=Xp5OvovX7IgrRBp8bcUM9MCo7Kne0r4i075VTbsoTls&s=a2Xp_KW3lx6QItxNr4KIsWlSKhgdDXj8TBmwQgkJDxQ&e= >>> ________________________________ >>> From: andre maute <and...@gm...> >>> Sent: Wednesday, May 1, 2019 5:55:44 PM >>> To: <max...@li...> >>> Subject: [Maxima-discuss] using return within a for loop within a >> function >>> >>> Hi list, >>> >>> this one baffles me most of the times when prototyping >>> mathematical code in Maxima respectively C++ >>> and converting between the two languages. >>> >>> We have the following two snippets, where the >>> first one outputs "1" and the second outputs "-1" >>> >>> Do I miss something here? >>> I often resort in these situations to >>> a solution with extra flags in the Maxima code. >>> >>> Regards >>> Andre >>> >>> >>> ---testreturn.mac--- >>> display2d : false; >>> >>> my_foo(d1,d2) := block( >>> >>> [res:-1,m], >>> >>> for m : 1 thru d2 do block( >>> if (m = d1) then block( >>> return(res) >>> ) >>> ), >>> >>> return(d1) >>> ); >>> >>> print( my_foo(1,10) ); >>> >>> ---testreturn.mac--- >>> >>> ---testreturn.cpp--- >>> #include <iostream> >>> >>> int foo(int d1, int d2) >>> { >>> int res = -1; >>> >>> for(int m = 1; m <= d2; m++) { >>> if(m == d1) { >>> return(res); >>> } >>> } >>> >>> return(d1); >>> } >>> >>> int main() >>> { >>> std::cout << foo(1,10) << std::endl; >>> return 0; >>> } >>> ---testreturn.cpp--- >>> >>> >>> _______________________________________________ >>> Maxima-discuss mailing list >>> Max...@li... >>> >> https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.sourceforge.net_lists_listinfo_maxima-2Ddiscuss&d=DwICAg&c=Cu5g146wZdoqVuKpTNsYHeFX_rg6kWhlkLF8Eft-wwo&r=Ln0CjFotuA7GyhsRS-QpQA&m=Xp5OvovX7IgrRBp8bcUM9MCo7Kne0r4i075VTbsoTls&s=a2Xp_KW3lx6QItxNr4KIsWlSKhgdDXj8TBmwQgkJDxQ&e= >>> >> >> >> >> _______________________________________________ >> Maxima-discuss mailing list >> Max...@li... >> https://lists.sourceforge.net/lists/listinfo/maxima-discuss >> > |