Remarks
- Using an internal function, recursion, and a trampoline is a good way to structure recursive procedures on a lists.
- The code fragment
(+ accu (- (first lst) accu)))
is the same as (first lst)
.
- The name
accu
suggests an accumulator, however, the value it stores is the maximum, so max
might be better.
- Because of the letter
l
can be confused with 1
, lst
can be read as slang for 'first'. I prefer xs
as the name for a list of x
's. The nomenclature has become more common in Lisps over the past few decades due to the influence of other functional languages.
- No matter how it is dressed up, there's a
max
procedure in the code somewhere. And it has to be written. It is better to be explicit about it than to try to hide max
by embedding it in some other function.
Alternative
For procedures that try to find a property of a list, folding is often a good place to start. This code uses foldl
. Usually I find foldl
rather than foldr
is what I want, for whatever that's worth.
#lang racket
(define (highest-number xs)
(define (max x1 x2)
(if (> x1 x2) x1 x2))
(foldl max (first xs) (rest xs)))
Maybe it's worth pointing out, that the starting point for writing the highest-number
was wishful thinking where I wished I had max
. Then at the next lower level of abstraction down, I wrote it. Credit goes to Ableson and Sussman's SICP lectures.