loops - collecting multiple maximum values -
i have list of elements. each element structured followed:
('symbol "string" int-score)
an example list:
(list (list 'object1 "wabadu" 0.5) (list 'object2 "xezulu" 0.6) (list 'object1 "yebasi" 0.5) (list 'object1 "tesora" 0.2))
i want retrieve maximum values specific symbol. when search symbol object2
, should back:
('object2 "xezulu" 0.6)
if search object1
, should back:
(('object1 "wabadu" 0.5) ('object1 "yebasi" 0.5))
i want collect highest elements of specific object. can this: assume above list list used below , i'm searching object1
. can retrieve elements of specific object:
(loop element in list when (equal 'object1 (first element)) collect element)
i can retrieve 1 highest element of list:
(loop element in list when (equal 'object1 (first element)) maximize (third element))
however, return 1 element. want all maximum elements. i've tried combinations collect
, maximize
, knowledge on syntax little. there way collect highest elements in ‘simple’ function?
you can loop
ing through list once selecting sublists right first elements , determining maximum (you can use into
let loop
accumulate multiple values), , second loop
in finally
clause go through selection , select maximum score:
(loop triple in *l* (key nil score) = triple when (eq key 'object1) collect triple selection , maximize score max-score (return (loop triple in selection when (eql (third triple) max-score) collect triple)))
edit: alternatively, instead of second loop, delete
function can used here quite concisely:
(loop triple in *l* (key name score) = triple when (eq key 'object1) collect triple selection , maximize score max-score (return (delete max-score selection :test #'/= :key #'third)))
Comments
Post a Comment