I am currently reading the book "Realm Of Racket", which I really like so far. However, in chapter 4 1/2, on page 74, there is one code example that I just don't get. Maybe my mind refuses to figure it out because I am on vacation, however, I simply don't get what it does.
(define (winners lst pred)
(cond
[(empty? lst) (list pred)]
[else
(define fst (first lst))
(if (score> (record-score pred) (record-score fst))
(list pred)
(cons pred (winners (rest lst) fst)))]))
They don't really explain it in the book. They give some hints, though:
- "The purpose of the function is to pick the first-place finishers from a list of game records."
- "We have a struct definition of the following shape: (struct record (name score))"
- "lst is a list of such records, and pred is one such record. Indeed, the original list is (cons pred lst), and it is sorted according to score."
- "You understand that winners is a list-eating function and goes through one record at a time. When there is at least one other record, it picks the first one, names it fst, and compares the scores of fst and its predecessors. Depending on the outcome, all winning records have been picked off or winners has to continue the search for equal-scoring players."
I suppose that score>
is a typo. Besides that I fully understand the code - in terms of syntax and semantics. I just don't get the practical use of that. What is this and why would somebody want that?