Post Reply 
HP48 programming: use values from stack in expression
12-05-2018, 03:49 PM
Post: #10
RE: HP48 programming: use values from stack in expression
Another interesting use of MATCH is its combination with UDF (User Defined Function) versions of RPN-only commands, allowing you to select sub-expressions within larger expressions as arguments for those rewritten commands. In other words, it allows you to perform RULES-type operations right on the stack or inside programs. Here's an example with COLCT.

Code:
@ NAME     : SCOLCT
@ ABSTRACT : Performs selective COLCT on a specified sub-expression.
@            SCOLCT takes a symbolic expression 'symb1' from stack level 2 and
@            a symbolic pattern 'symb.pat' from stack level 1 and performs a
@            selective COLCT within 'symb1' on the sub-expression specified by
@            the wildcard name '&.' in 'symb.pat' ("." was chosen because it
@            lies on a primary key and is hardly ever used as a variable name).
@            Names are left unevaluated, whether a corresponding variable
@            exists in the current path or not; in other words, they are
@            treated as formal variables.
@ STACK    : ( 'symb1' 'symb.pat' --> 'symb2' )
@ EXAMPLES : ( '2*X+3*X-COS(4*X+5*X)+SIN(6*X+7*X)' 'COS(&.)' -->
@                                             '2*X+3*X-COS(9*X)+SIN(6*X+7*X)' )
@            ( 'SIN(2*X+3*X)/(4*X+5*X)' '&1/&.' --> 'SIN(2*X+3*X)/(9*X)' )
\<<
  \<< \-> x \<< x COLCT \>> \>>   @ Build embedded UDF (User Defined Function).
  \-> f                           @ Save UDF in local variable.
  \<< DUP \->STR                  @ Decompile copy of 'symb.pat'.
      DUP "&." POS                @ Find "&." token.
      OVER OVER 1 - 1 SWAP SUB    @ Get head part of string without "&." token.
      "f(&.)" +                   @ Replace "&." with "f(&.)".
      ROT ROT 2 + MAXR \->NUM SUB @ Get tail part of string.
      +                           @ Append tail part.
      OBJ\->                      @ Recompile string; use it as 'symb.repl'.
      2 \->LIST                   @ Build list argument for MATCH.
      \|^MATCH DROP               @ Replace. (Use \|vMATCH for top-down repl.)
      #64037h SYSEVAL             @ Prevent global name resolution by executing
                                  @   SysRPL word WithHidden, which temporarily
                                  @   sets both context and stopsign to the
                                  @   Hidden Directory for the duration of the
                                  @   next object's (EVAL) execution.  N.B.
                                  @   WithHidden is a supported entry point.
      EVAL                        @ Perform selective COLCT by applying UDF;
                                  @   reset context & stopsign to current dir.
  \>>                             @ Discard UDF.
\>>
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: HP48 programming: use values from stack in expression - Giuseppe Donnini - 12-05-2018 03:49 PM



User(s) browsing this thread: 1 Guest(s)