Proof of my lack of brilliance
I’ve been reading through Structure And Interpretation Of Computer Programs in the evenings, and have started going through the exercises as well. I figure it’ll help bring home the finer points of the text, and it’s sorta like little puzzles to work on, plus i’m learning scheme, and since I have a well documented affection for lisp it works out well.
One of the first exercises that they recommended was given three numbers square the sums of the two greater numbers. Pretty straight-forward right? Yea..pretty much. I implemented the usual if (and (> a b) (> b c)) then A is the biggest number. Pretty easy to find the largest and smallest numbers with that approach. However, start coding up for the middle number and you end up with this mess of unreadable garbage, well not unreadable but you end up wishing there was something elegant you could like the above statements.
After a bit of thought I hit upon it it goes something like this (- (+ a b c) (+ (get-max-num(a b c)) (get-min-num(a b c))) which works out rather well because the both the max and min of three numbers are very easy found using just a couple of lines of code. This is obvious when you think about it, but when you’re coming from a bunch of >, <, and, cond statements it’s not the thing you would consider at first..i guess…well i didn’t.
I mentioned this to shad and his response was something along the lines of “duh, obviously b = (a + b + c) – (a + c) it’s algebra”. This response further reinforces the theory that i’m in over my head most of the time.