Post Reply 
HP 48G - Edit List
08-23-2021, 03:13 AM
Post: #6
RE: HP 48G - Edit List
(08-23-2021 12:52 AM)John Keith Wrote:  
(08-23-2021 12:02 AM)MNH Wrote:  I wonder why a command was never written that edits a list in the way I want.

Most likely lack of ROM space. The 48G does add many useful features for list processing compared to the 28 or 48S but certainly there can't be enough space for every imaginable command. For the 50g there are libraries such as GoferLists and ListExt which extend list processing functions well beyond those of the 48 series.

(Emphasis added by me Smile ) I think you nailed it with the space issue. List manipulation in RPL code is inherently slow due to the free-form nature of the data structure (a prologue, data objects in sequence, a token to signify the end of the data). Accessing individual elements requires stepping through the entire sequence that precedes the element in question. Speeding things up requires Saturn code, which in turn requires a lot of space.

(08-23-2021 12:52 AM)John Keith Wrote:  Regarding DEPTH I did not mean to imply that it is an evil command that should not exist, but that its use in programs can be problematic. DEPTH is often useful in interactive calculations or in quick-and-dirty programs for one-off use, but questionable for most programs, especially ones that may be called by other programs.

I agree that the use of DEPTH as presented in the original program above is problematic. It limits the usefulness of the program to situations where there is nothing else on the stack other than arguments to that function, which means it's not very accommodating.

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.
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)