Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: "B. Pym" Newsgroups: comp.lang.lisp Subject: Re: finding the min or max element of a list Date: Thu, 18 Jul 2024 08:27:06 -0000 (UTC) Organization: A noiseless patient Spider Lines: 33 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Injection-Date: Thu, 18 Jul 2024 10:27:07 +0200 (CEST) Injection-Info: dont-email.me; posting-host="8e507d2f4c44ea84c4d5df38107e66f0"; logging-data="2462794"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/uyB1KK5TyECkuvahXo+Iw" User-Agent: XanaNews/1.18.1.6 Cancel-Lock: sha1:LsHw86FrRH9+wZc0KfseyozvB30= Bytes: 1414 > (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