SICP 2.2.3 Exercises
July 7, 2014 10:23
From abstract data structures that can be manipulated at a higher level than just the lists or pairs, we move on to common interfaces more generally. As detailed in the text, finding the connection between similar problems that allows them to be treated with a common interface requires a certain amount of insight, but can often lead to a well-organized and simplified program structure. It’s often the case that such programs are not the initial approach to a problem (or set of problems) but can be found upon revisiting the code.
I’m personally of the opinion that as clever and concise as this syntax can often be, it can also lead to code that needs some unraveling to understand later on. But in some cases it is a natural way to express a formula, and this sort of interface can be used in a powerful way (which we get to later in the text). Making the choice of when to use it is as much about personal style as it as about clarity and efficiency.
One of the easiest ways to test these procedures is to make sure that we’re doing the same as expected for the versions not defined in this way. In the case of
length, we can compare the output directly by using alternate names for our new routines (this is also necessary, since 2.37 requires the original version of
map that can take multiple lists). Most of the other procedures will just output the result of calculation, as we don’t always have another version to compare to.
The queens problem (2.42) requires an additional step of creating a procedure
print-solution. This can be as complex or simple as you like, but given that there may be a large number of solutions for some board sizes, creating something that will make the output more readable than a long series of numbers may be helpful.