Post Reply 
Depth of nested list
09-11-2023, 03:28 PM
Post: #10
RE: Depth of nested list
(09-11-2023 12:55 PM)DavidM Wrote:  
Code:
...    LIST\->                 @ explode current list
    1 SWAP START            @ loop for each object in current list
...

And without even trying, I proved what I told John above about half-thought-out program ideas.

The program I posted fails miserably if the source list is either empty or contains an embedded list which is empty (the code snippet above shows why).

A simple way to fix that is to simply make sure that each list has at least one value, since the quantity and type of non-list items doesn't really matter. A fairly simple fix (which unfortunately contributes to the program's ugliness) is to just add something to each list encountered. I do this below by adding NOVAL to each encountered list:
Code:
\<<
  1 DUP                     @ current/max depth defaults
  {                         @ main subroutine opening delimiter
    NOVAL +                 @ add an element to insure non-empty list
    LIST\->                 @ explode current list
    1 SWAP START            @ loop for each object in current list
      DUP TYPE 5 SAME       @ is the current object a list?
      {                     @ yes, it's a list (THEN clause)
        ::\<-c INCR         @ increment current depth
        \<-m OVER <         @ check for new max depth being reached
        { ::\<-m STO }      @ yes, it's the new max depth (THEN clause)
        ::DROP              @ no, not a new max depth (ELSE clause)
        IFTE                @ if <SL3> then <SL2> else <SL1>
        \<-s EVAL           @ process current object as list
        ::\<-c DECR DROP    @ current depth completed, so decrement
      }                     @ end of processing for list object
      ::DROP                @ object was not a list, so just drop it
      IFTE                  @ if <SL3> then <SL2> else <SL1>
    NEXT                    @ process the next object
  }                         @ end subroutine delimiter
  \-> \<-c \<-m \<-s        @ local names (current depth, max depth, subroutine)
  \<<                       @ program to execute with locals in-scope
    \<-s EVAL               @ process the given list in SL1
    \<-m                    @ output the maximum depth encountered
  \>>
\>>

I dislike this kind of patch, and in practice I would likely come up with a better solution for something like this. For now, it will suffice. Smile
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Depth of nested list - rickster - 09-08-2023, 11:08 PM
RE: Depth of nested list - Joe Horn - 09-09-2023, 12:07 AM
RE: Depth of nested list - John Keith - 09-09-2023, 10:34 AM
RE: Depth of nested list - DavidM - 09-09-2023, 11:33 AM
RE: Depth of nested list - John Keith - 09-09-2023, 03:00 PM
RE: Depth of nested list - DavidM - 09-09-2023, 04:15 PM
RE: Depth of nested list - Albert Chan - 09-09-2023, 11:59 AM
RE: Depth of nested list - John Keith - 09-09-2023, 05:22 PM
RE: Depth of nested list - DavidM - 09-11-2023, 12:55 PM
RE: Depth of nested list - DavidM - 09-11-2023 03:28 PM
RE: Depth of nested list - Gilles - 09-11-2023, 04:05 PM
RE: Depth of nested list - Werner - 09-11-2023, 05:46 PM



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