[plt-scheme] simple scheme idiom for generalized maximum?
(define (longest-string los)
(first (sort los > #:key string-length)))
On Sep 23, 2009, at 2:08 PM, John Clements wrote:
> 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"
>
>
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme