• 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. Using max() on a list

Stats

  • Locked Locked
  • Replies 6
  • Subscribers 143
  • Views 18652
  • 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

Using max() on a list

Yaosan
Yaosan over 15 years ago

I'm trying to get the maximum value from a list of numbers but the max() function doesn't work on list i.e. it needs each elements from the list to be defined as an argument. How can I get around this?

  • Cancel
  • skillUser
    skillUser over 15 years ago

     Hi Yaosan,

    You can use the apply function, as in the following example:


    mylist = list( 1 3 9 27 )
    apply('max mylist)
    => 27

    I hope that it helps you!

    Regards,

    Lawrence.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Yaosan
    Yaosan over 15 years ago
    Amazing, I didn't know about the apply function, thanks for the prompt reply Lawrence, it works great!
    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • dmay
    dmay over 15 years ago

    If you are dealing with really long lists,  (like over 60,000 items), you can run into a stack overflow using the apply on these lists. You can always use a sort:

    mymax = car(sort(mylist 'greaterp))

    Derek

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • eDave
    eDave over 15 years ago

    Sort on a copy of the list if you use that method and you need to retain the original list,

    car(sort(copy(mylist) 'greaterp))

    Dave

     

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • dmay
    dmay over 15 years ago

    Good point Dave. Sorts are "destructive" operations. Thanks for the clarification.

     Derek

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

    Note that using a sort to find the maximum is rather an expensive operation - you spend time and memory sorting all the elements, whereas all you want to do is find the largest.

    With 1 million entries in the list, the sort takes 4.6 seconds for me (on my laptop), and with the copy too, it uses 12 million bytes (that's all from the copy). The following:

    (defun abComputeMax (l)
      (let ((mx (car l)))
        (foreach val (cdr l) (when (greaterp val mx) (setq mx val)))
        mx
        )
      )
    

    takes 0.16 seconds, and 140 bytes. Yes, I know, it's not exactly a massive amount of time - but even so, it adds up.

    Regards,

    Andrew.

    • 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