[plt-scheme] simple scheme idiom for generalized maximum?

From: John Clements (clements at brinckerhoff.org)
Date: Wed Sep 23 14:08:42 EDT 2009

I'm sure this has been beaten to death somewhere, but I can't think of  
a natural way to find, e.g., the longest string in a list. It's easy  
to define maximumBy in terms of foldl1 (which I claim should be in  
there already), but seems like there should be an obvious way to do  
this using library functions.

Am I missing something obvious?

John


(define (foldl1 f l)
   (foldl f (first l) (rest l)))

(define (maximumBy f lst)
   (foldl1 (lambda (a b) (if (f a b) a b)) lst))

(maximumBy (lambda (a b) (> (string-length a) (string-length b)))
            `("abc" "abcd" "ab"))


=> "abcd"


-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2484 bytes
Desc: not available
URL: <http://lists.racket-lang.org/users/archive/attachments/20090923/52be42da/attachment.p7s>

Posted on the users mailing list.