From: Marieke
Subject: select four highest numbers from a list
Date: 
Message-ID: <37823127.6E529D38@cogsci.kun.nl>
How can I select the four highest scores from a list like '(1 2 3 3 5
6)??
The output needs to be '(3 3 5 6).
Can this be done easy with a primivitive like (highest amount list) ??
Thank you in advance.
Marieke
-- 
=============================
Marieke van der Linden
·······@cogsci.kun.nl
ICQ: 32669202
=============================

From: Barry Margolin
Subject: Re: select four highest numbers from a list
Date: 
Message-ID: <Itqg3.1149$KM3.276620@burlma1-snr2>
In article <·················@cogsci.kun.nl>,
Marieke  <·······@cogsci.kun.nl> wrote:
>How can I select the four highest scores from a list like '(1 2 3 3 5
>6)??
>The output needs to be '(3 3 5 6).
>Can this be done easy with a primivitive like (highest amount list) ??

(subseq (sort list) 0 4)

This probably isn't the most efficient way to do it for large lists,
though.  I'll bet it isn't the answer your professor is looking for in the
assignment, if this is homework.

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Lieven Marchand
Subject: Re: select four highest numbers from a list
Date: 
Message-ID: <m3btdpvdju.fsf@localhost.localdomain>
Marieke <·······@cogsci.kun.nl> writes:

> How can I select the four highest scores from a list like '(1 2 3 3 5
> 6)??
> The output needs to be '(3 3 5 6).
> Can this be done easy with a primivitive like (highest amount list) ??
> Thank you in advance.

You already asked in c.l.l and c.l.scheme. In which language do you want
a solution?

As a one-liner
(defun maak-je-eigen-huiswerk (results)
   (last (sort (copy-list results) #'<) 4))

In scheme it would be a bit longer.

-- 
Lieven Marchand <···@bewoner.dma.be>
If there are aliens, they play Go. -- Lasker
From: Pierre R. Mai
Subject: Re: select four highest numbers from a list
Date: 
Message-ID: <87emillprk.fsf@orion.dent.isdn.cs.tu-berlin.de>
Marieke <·······@cogsci.kun.nl> writes:

> How can I select the four highest scores from a list like '(1 2 3 3 5
> 6)??
> The output needs to be '(3 3 5 6).
> Can this be done easy with a primivitive like (highest amount list)
> ??

The problem is a little underspecified, it seems to me.  Do you want
to

a) Simply get the last n elements of an already sorted list (as the
   example seems to imply)?

   Then (last list n) is the simple solution (see HyperSpec).

Or do you want to

b) get the n highest (as specified by a predicate of your choice)
   elements of a list, regardless of the order of the given list?

   If speed is not important, you can always do something like this:

(defun highest (n list predicate &key (key #'identity))
  (let ((result (sort (copy-list list) predicate :key key)))
    (setf (cdr (nthcdr (1- n) result)) nil)
    result))

   This is a quick hack, there are probably better CL library
   functions to use (using subseq and copy-seq would give you a
   function that's usable on all sequences), and there is no
   error-checking.  Depending on your application stable-sort might
   be needed, or you may be able to omit the copy-list (beware: sort
   is destructive).

If however you want to

c) do the same as in b), but efficiently:  Get the nearest good book
   on algorithms and look up the chapter on sequence/sorting
   algorithms, where you will find advanced algorithms that can do the 
   above without sorting the list, IIRC.

> Thank you in advance.
> Marieke

Regs, Pierre.

-- 
Pierre Mai <····@acm.org>         PGP and GPG keys at your nearest Keyserver
  "One smaller motivation which, in part, stems from altruism is Microsoft-
   bashing." [Microsoft memo, see http://www.opensource.org/halloween1.html]