This was a surprisingly complex exercise because it took me a minute to figure out how to find the middle number given three numbers. We also lack complex data structures at this point (eg. arrays, lists). With >
and <
, it was easy to find the maximum and minimum, but the middle?, I was pleasantly challenged.
The best solution I found was to leverage the ease of finding the min and max values, together with using arithmetic. If you add all three numbers together, and subtract off the min and max values, then you'll be left with the middle value :).
(define (middle a b c)
(- (+ a b c)
(+ (largest a b c) (smallest a b c))))
Top comments (0)