WJ wrote:
> The easy way:
>
> (define (most2 func xs)
> (apply values (map last
> (take
> (sort (map (lambda(x)(list (func x) x)) xs) > #:key car)
> 2))))
That has two defects. It crashes when the list has fewer than
2 items, and it's not simple enough.
(define (most2 func xs)
(apply values
(take
(sort xs > #:key func #:cache-keys? #t)
(min 2 (length xs)))))
If the list has fewer than 2 items, then fewer than 2 values
are returned.
Using cache-keys? ensures that func is called only once for
each item.