Home > Community > Forums > Custom IC SKILL > Using max() on a list

Email

* Required Fields

Recipients email * (separate multiple addresses with commas)

Your name *

Your email *

Message *

Contact Us

* Required Fields
First Name *

Last Name *

Email *

Company / Institution *

Comments: *

 Using max() on a list 

Last post Thu, Feb 11 2010 1:13 AM by Andrew Beckett. 6 replies.
Started by Yaosan 10 Feb 2010 10:01 PM. Topic has 6 replies and 2357 views
Page 1 of 1 (7 items)
Sort Posts:
  • Wed, Feb 10 2010 10:01 PM

    • Yaosan
    • Top 500 Contributor
    • Joined on Fri, Aug 7 2009
    • Toronto, Ontario
    • Posts 35
    • Points 610
    Using max() on a list Reply

    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?

    Filed under:
    • Post Points: 20
  • Wed, Feb 10 2010 10:07 PM

    • skillUser
    • Top 10 Contributor
    • Joined on Fri, Sep 19 2008
    • Austin, TX
    • Posts 2,587
    • Points 15,915
    Re: Using max() on a list Reply

     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.

    Filed under: , ,
    • Post Points: 20
  • Wed, Feb 10 2010 10:15 PM

    • Yaosan
    • Top 500 Contributor
    • Joined on Fri, Aug 7 2009
    • Toronto, Ontario
    • Posts 35
    • Points 610
    Re: Using max() on a list Reply
    Amazing, I didn't know about the apply function, thanks for the prompt reply Lawrence, it works great!
    • Post Points: 20
  • Wed, Feb 10 2010 10:49 PM

    • dmay
    • Top 25 Contributor
    • Joined on Thu, Jul 17 2008
    • Allen, TX
    • Posts 394
    • Points 7,420
    Re: Using max() on a list Reply

    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

    • Post Points: 20
  • Wed, Feb 10 2010 11:10 PM

    • eDave
    • Top 10 Contributor
    • Joined on Sun, Jul 13 2008
    • Christchurch, 00-NZ
    • Posts 738
    • Points 16,055
    Re: Using max() on a list Reply

    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

     

    Dave Elder, Tait Communications
    • Post Points: 20
  • Wed, Feb 10 2010 11:16 PM

    • dmay
    • Top 25 Contributor
    • Joined on Thu, Jul 17 2008
    • Allen, TX
    • Posts 394
    • Points 7,420
    Re: Using max() on a list Reply

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

     Derek

    • Post Points: 20
  • Thu, Feb 11 2010 1:13 AM

    Re: Using max() on a list Reply

    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.

    • Post Points: 5
Page 1 of 1 (7 items)
Sort Posts:
Started by Yaosan at 10 Feb 2010 10:01 PM. Topic has 6 replies.