Jump to content

Maximum value of the list


Dayananda

Recommended Posts

a = (100 125 10 97)

Now how can I get the maximum value  of above list (max a) does not works.

But (car a) = 100 it can get

 

Edited by Dayananda
Link to comment
Share on other sites

49 minutes ago, Dayananda said:

a = (100 125 10 97)

Now how can I get the maximum value  of above list (max a) does not works.

But (car a) = 100 it can get

 

 

 

(apply 'max a)

 

  • Thanks 1
Link to comment
Share on other sites

(apply 'max a) will always be the quickest, but will be susceptible to the inherent limitation in the number of arguments that you can supply to the min or max functions, and will therefore fail for long lists; other alternatives which avoid this limit might be:

(car (vl-sort a '>))

But sorting is also unnecessarily slow - on average on the order of O(n log(n)) when only a maximum is required, and so this method should be avoided.

 

Another alternative might be:

(setq r (car a))
(foreach x (cdr a) (if (< r x) (setq r x)))

 

  • Thanks 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...