• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Custom IC SKILL
  3. how to get depth of list

Stats

  • Locked Locked
  • Replies 5
  • Subscribers 143
  • Views 14933
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

how to get depth of list

lawlag02
lawlag02 over 13 years ago

Hi evreyone

does anyone here knows how to get depth of list by SKILL in a very simplistic way. let's say a flat list depth = 0, then if i have something like

A = (((1 2 3) 4 5) 6 7 8 )

this should give me depth of A = 2

many thanks in advance for any solution

 BR  law

  • Cancel
  • Andrew Beckett
    Andrew Beckett over 13 years ago

    A simple recursive function will do this:

    procedure(abListDepth(lst @optional (depth 0) "lx" )
      let(((newDepth depth))
        foreach(elem lst
          when(listp(elem)
            newDepth=max(newDepth abListDepth(elem add1(depth)))
          )
        )
        newDepth
      )
    )

    Regards,

    Andrew.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • lawlag02
    lawlag02 over 13 years ago

    Hi Andrew,

    Thanks a lot. As always, It works perfectly. :-D

    Best regards,

    law

     

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • ebecheto
    ebecheto over 13 years ago

     I had it also, but without foreach loop. I suppose add1 is faster than var+1 ?

     

    A='(((1 2 3) 4 5) 6 7 8)
    
    (defun carDepth (lst @optional (i 0))
    (cond
    ((null lst) i)   ;=> nothing in the list anymore = stop condition
    ((atom (car lst)) printf("Parse i:%d : %L\n" i lst) (carDepth (cdr lst) i))  ; when first element is single process it (nothing) and go further is returned
    (t                 (max (carDepth (car lst) i+1 ) (carDepth (cdr lst) i))) ; else append head and tail with the function itself recursing
    ))
    
    carDepth(A) ;=> 2
    

     You may remove the printf statment

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 13 years ago

    The difference between +1 and add1() is miniscule (in fact from a quick profile I just did with 100 million adds, it made a difference of less than 2 seconds, and in fact add1() was slower - but I didn't check to ensure it was statistically significant, but it wouldn't be worth worrying about for efficiency because the difference is so tiny.

    However, the approach you have taken (which is a very "SCHEME" way of doing things), whilst simple, does suffer from the fact that the extreme use of recursion will mean you are much more likely to hit SKILL's stack limit:

    I ran with a list with 10000 elements in it, and I get:

    *Error* unknown: Runtime Stack Overflow!!!

    In my approach, I was only using recursion for the actually nested hierarchy of the list, not the length of the list. Mapping (i.e. foreach) doesn't have this problem.

    Andrew.

     

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • ebecheto
    ebecheto over 13 years ago

    It is always nice to have your wise point of you!

    Thanks

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information