• Re: finding the min or max element of a list

    From B. Pym@21:1/5 to All on Thu Jul 18 08:27:06 2024
    (defun best (lst cmp &key (key #'identity))
    (loop with rtn = (car lst)
    for x in (cdr lst)
    when (funcall cmp (funcall key x) (funcall key rtn))
    do (setq rtn x)
    finally (return rtn)))

    Gauche Scheme

    (define (best lst cmp :key (key identity))
    (reduce
    (lambda (x chosen)
    (if (cmp (key x) (key chosen))
    x chosen))
    #f
    lst))

    (best '((a) (f o o b) (b a r) (2 3)) > :key length)
    ===>
    (f o o b)

    (best '(5 0 4 9) >)
    ===>
    9

    (best '() >)
    ===>
    #f

    (best '(2) >)
    ===>
    2

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)