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

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Sat Jan 18 14:43:10 EST 2014

Hi Gustavo,

An efficient solution for large data sets is to use heaps:

#lang racket
(require data/heap)

(define data '((3 "three") (4 "four") (2 "two") (1 "one")))

(define h (make-heap (λ(x y) (<= (first x) (first y)))))
(heap-add-all! h data)
(heap-min h)

/Jens Axel


2014/1/18 Gustavo Massaccesi <gustavo at oma.org.ar>:
> 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



-- 
--
Jens Axel Søgaard


Posted on the users mailing list.