Quirksand

SICP

SICP 2.2.1 Exercises

May 7, 2014 14:18

After tackling pairs, we move on to a data structure that is even more useful, the list. Certainly if you are working in the LISt Processing language (or in a dialect such as Scheme), you expect that comprehending how lists work is important, and you wouldn’t be wrong. That said, the particular choice of language isn’t the important part; it’s the fact that we’re building up our knowledge of how the data structures of the language actually work.

We’re still working with numeric functions, which remain easy to test. Something that’s prominent in this set of exercises is a variation on a particular procedure, including those that are working and non-working. I’ve written a test function that, when run, will display the results of working on a preselected set of lists. This just shows the results rather than testing values, which makes it easy to compare the answers that each variation yields. It does depend on the ability to redefine procedures, as it calls the square-list procedure by name each time.

At the end of the exercises we come to for-each. This is a highly useful procedure that allows us to iterate on a given list quite easily with any function we like. It makes it relatively easy to test a set of values without having to write a new line for each value, and will come in quite handy. Since for-each should be built into the language, you won’t have to define it every time in the future. That also means that for this section you’ll need to be able to redefine it in whatever dialect you’re using (alternately, rewrite the tests, but if you really can’t redefine the built-in functions, you may wish to switch environments, as this problem is only going to get harder to work around).

Exercises 2.2.1