Re: collect-max for structs

Liste des GroupesRevenir à cl lisp 
Sujet : Re: collect-max for structs
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp
Date : 16. Jun 2025, 13:50:47
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <102p3v4$1ktql$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
(loop for i in *applelist*
  maximizing (apple-radius i) into max
  finally (return max))
 
This  can be written simpler:
 (loop for i in *applelist* maximizing (apple-radius i))
or
 (reduce #'max *applelist* :key #'apple-radius)
Unfortunately I don't think there's really any elegant way to solve
your problem. It boils down to something like
 
 (loop with biggest-apple = (first *applelist*)
    for i in (rest *applelist*)
    do (when (> (apple-radius i) (apple-radius biggest-apple))
         (setf biggest-apple i))
    finally (return biggest-apple))
--
Frode Vatvedt Fjeld
 
I hadn't seen reduce before. The following function solves my problem
rather nicely. Thanks for the idea, Frode.
 
(defun biggest (lst)
  (reduce
    (lambda (x y)
      (if (bigger x y) x y))
    lst))

Gauche Scheme

(use gauche.collection)  ;; find-max

(find-max '("to" "and" "four" "o") :key string-length)
  ===>
"four"

Date Sujet#  Auteur
16 Jun 25 o Re: collect-max for structs1B. Pym

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal