Algebraic programming on HP50G - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html) +--- Forum: Articles Forum (/forum-14.html) +--- Thread: Algebraic programming on HP50G (/thread-15236.html) Algebraic programming on HP50G - Lucasiewicz - 06-21-2020 03:57 PM For those who still like to play with the HP49/50 here are two tips that can not even be found in the paper about algebraic versus RPN programming by De Graeve and Parisse Tip 1 In their examples they use recursion and say that you need to store the function/program under the same name as a global variable There's a simple way to do away with such an unnecessary limitation and implement recursion in a clean lambda that can be stored in any name or executed directly from the stack without any external reference Assign the program to a local variable with the name of the function and leave the parameters on the stack for evaluation Here's an example of a recursive implementation of Factorial « « -> N « IF `N>2` THEN `N*F(N-1)` ELSE N END » » -> F « F EVAL » » Note that although this program uses pure algebraic code in the functional path it is an RPL program But don't worry, the calculator is smart enough to recognize it and will take care of this in either mode in the editor Tip 2 Those who like the challenge (or the torture) of pure algebraic programming De Graeve's paper states that in an algebraic program you need to create a nested scope block for every new local variable Syntactically you can indeed only define 1 local variable for every scope but there's a simple coding pattern that allows you to use an unlimited number of local variables without having to clutter your program with countless unnecessary nested scopes Here's how it looks and this pattern can be used and edited in both RPN and ALG mode (you need to explicitly enter the back tick in RPN mode) « -> P1 P2.... Pn « `EVAL(0, 0, ..., 0, « -> L1 L2... Ln « your code » »)` » » The EVAL command passes all the initializer values to the scope which is its last parameter where they are used to initialize all the defined local variables This pattern is extendable to any number of input parameters and local variables without the need of extra scope clutter This pattern can be stored as a template and used for every new pure algebraic program you create in ALG as well as RPN mode I'll give a program to calculate Fibonacci as an example « -> N « `EVAL(0,1,0, « -> T F1 F2 « WHILE (N-1|>N)>0 REPEAT F2|>T ; F1+F2|>F2 ; T|>F1 ; END ; F2 » »)` » » The |> is the algebraic STO command Using what we learned here we can rewrite the program from tip 1 and make it pure algebraic, although this modification will make it less generic as the number of parameters of the encloing scope and the EVAL must now correspond to those of the function /program « -> M « ` « -> N 'IFTE(N>2,N*F(N-1),N)' »` -> F « `EVAL(M,F)` » » » Hope it's still useful after all these years that RPL has been decommissioned