[racket] (define best (compose car sort))

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sat Jan 18 15:46:42 EST 2014

Do you want argmax 


On Jan 18, 2014, at 11:57 AM, Gustavo Massaccesi wrote:

> I extended one of the Rosetta code task: Levenshtein
> distance/Alignment:
> http://rosettacode.org/wiki/Levenshtein_distance/Alignment to show the
> alignment.
> 
> I needed a function that finds the “best” item in a list. It’s like
> sort but it only finds the first one:
> 
> #lang racket
> (define best (compose car sort))
> 
> (define data '((3 "three") (4 "four") (2 "two") (1 "one"))
> 
> (best data < #:key car) ;==> (1 "one")
> (best data > #:key car) ;==> (4 "four")
> 
> (best data string<? #:key cadr) ;==> (4 "four")
> (best data string>? #:key cadr) ;==> (2 "two")
> 
> It’s not very efficient, but I needed it only for n=3.
> 
> I could have written an efficient version by hand, but I it was only
> an auxiliary function and I wanted to keep the task as short as
> possible.
> 
> Did anyone use a similar function in another program? Can the
> efficient version be added to the standard library? (Is it a good idea
> to add yet another function to the standard library?)
> 
> Gustavo
> 
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users



Posted on the users mailing list.