[plt-scheme] simple scheme idiom for generalized maximum?
Don't forget argmax
Jay
On Wed, Sep 23, 2009 at 12:12 PM, Matthias Felleisen
<matthias at ccs.neu.edu> wrote:
>
> (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
>
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
--
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://teammccarthy.org/jay
"The glory of God is Intelligence" - D&C 93