|Original post by Muhammad Haggag|
Exercise 2.35 is about writing a count-leaves procedure as an accumulation operation. I wrote a recursive solution as follows:
*** Source Snippet Removed ***
Is it possible to get away with a simpler solution? (Not that this one is complex--it isn't. Just wondering)
Kinda hard to answer. In theory, there could be a accumulate
function specialized for trees and you could use that. But this logic would lead to absurdities, as you could simply say there's an even more specialized pre-existent function doing exactly the same as what count-leaves
is supposed to do, and just call that. That would definitely lead to the simplest code.
So, using accumulate
as it's defined in SICP, your code is probably the simplest solution, simplest meaning compact without being cryptic. Maybe you could make it a bit clearer by separating levels of abstraction (by using helper functions), but that's definitely nitpicking. Plus, this separating could lead to very abstract functions which would make things more complicated against.
Time to settle on a single answer for once: "hmmmm... I'm not sure".