• Re: collect-max for structs

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp on Mon Jun 16 12:50:47 2025
    From Newsgroup: comp.lang.lisp

    (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"
    --- Synchronet 3.21d-Linux NewsLink 1.2