Post Reply 
HP 48G - Edit List
08-23-2021, 09:41 PM
Post: #7
RE: HP 48G - Edit List
(08-23-2021 03:13 AM)DavidM Wrote:  That said, DEPTH is still very useful. The internal version of the DEPTH command is likely to be one of the most used segments of code in an RPL calculator, since pretty much every UserRPL command does an initial check of the stack depth to make sure that at least the correct number of arguments exists prior to execution. This is part of the normal structure of SysRPL code. I've rarely seen User RPL code designed to do an initial check for argument counts (and/or types), but DEPTH could be used for that purpose. I'm not advocating for people to write User RPL code in that way, mind you, just saying that it could be done. The fact that built-in commands always do this for you means that it isn't as important to do it yourself.

One of the best uses of DEPTH in User RPL code that I've seen is in comparing "before" and "after" counts of stack entries when you don't know in advance how many new stack entries may be added by some process. Consider the following program, which provides a list of the results from applying the even/odd functions in sequence for a Collatz Conjecture implementation:
Code:
\<<
   DEPTH 1 - \->  notmine     @ 1 argument on the stack is mine, the rest should be left alone
   \<<
      WHILE
         1 OVER SAME NOT      @ repeat as long as current value is not 1
      REPEAT
         IF
            DUPDUP 2 MOD      @ 0: even, 1: odd
         THEN
            3 * 1 +           @ odd: 3x+1
         ELSE
            2 /               @ even: x/2
         END
      END
      DEPTH notmine - \->LIST @ put all new stack entries into result list
   \>>
\>>

The program can't know in advance how many entries will be created, so it simply finds the delta for the before and after stack depths, and puts that count of objects into the result list. This is in fact the same method used by several of the built-in list commands for "knowing" how many objects to include in result lists.

That is indeed a clever method, consider it stolen!
For programs like that, I have generally maintained a counter on level 1 and SWAP objects above the counter as they occur, then increment the counter. At the end, \->LIST puts all of the objects into a list.

Your method is undoubtedly faster since it avoids all of the swapping and incrementing.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
HP 48G - Edit List - MNH - 08-22-2021, 03:40 AM
RE: HP 48G - Edit List - John Keith - 08-22-2021, 03:02 PM
RE: HP 48G - Edit List - MNH - 08-23-2021, 12:02 AM
RE: HP 48G - Edit List - John Keith - 08-23-2021, 12:52 AM
RE: HP 48G - Edit List - DavidM - 08-23-2021, 03:13 AM
RE: HP 48G - Edit List - John Keith - 08-23-2021 09:41 PM
RE: HP 48G - Edit List - John Keith - 08-23-2021, 01:13 AM



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