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

From: Gustavo Massaccesi (gustavo at oma.org.ar)
Date: Sat Jan 18 11:57:37 EST 2014

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


Posted on the users mailing list.