Structure and
Interpretation
of Computer
Programs

Fall 2021
course
site
-->

Thu Sep 23

Questions and/or discussion of any of the homework problems ... we went over much of this last Monday. I've posted my solutions to the assigned problems in the "Jim's exercises" folder ; please do compare with your work.

in class : reverse

Let's continue the thread that we were working on during Monday's class : reversing a list. (This is exercise 2.18 in the text.)

Implement `(reverse items)` so that for example
`(reverse (list 1 2 3 4))` gives `(4 3 2 1)`.

Take some time to try this on your own or with a neighbor, then we'll discuss.

These UTexas notes go over some of the issues, and discuss one way to find a solution using append that we did last time ... but its not an efficient solution :

(define (reverse items)
   (if (null? items)
       '()
       (append (reverse (cdr items))
               (list (car items)))))

How does that work? Why is it slow? Answer: append is O(n) to walk to the end of the first list , and there will be O(n) (append ... (append ... (append ...))), so that means this whole thing will be a loop within a loop, which is O(n**2).

The efficient approach walks the list once, building up a new one. We did it in class, and I've attached that code.

this week

This week we're continuing to discuss "higher order functions" - functions of functions.

The text does this in both chapter 1 (where they're only talking about numbers and functions that operate on numbers) and chapter 2 (where they are working with lists and trees).

I've posted the assignment, and will give a (hopefully brief) overview of the accumulate idea, and then let you start in.

The idea is that can generalize an expression like

 1/2 + 1/3 + 1/4 + 1/5 + 1/6

where we are combining a number of terms according to a specific pattern .

Exercise 1.32 asks you to express this as

 (accumulate combiner null-value term a next b)

or as I'd rather name the terms

 (accumulate op nothing func start step stop)

For this example we have

 op            +
 nothing       0
 func          1/x
 start         2
 step          add 1
 end           5

In chapter 1 of the text, we need the "step" function to generate a sequence - the authors haven't yet covered lists.

But more commonly this kind of operation is thought of as being applied to a list of items; accumulate is then usually called fold, and is a powerful building block for many other ideas, not just related to numbers.

So for example

(fold + 0 '(1 2 3 4 5))

might turn into something like

(+ 1 (+ 2 (+ 3 (+ 4 (+ 5 0)))))  ;; i.e. (1 + (2 + (3 + (4 + (5 + 0))))

And since we just learned that a list in scheme is

(cons 'a (cons 'b (cons 'c '()))

you should see that we have the same pattern ... which we can therefore extract into a new idea.

It turns out that there are two different variations of this idea, fold-left and fold-right ... which we will explore in this week's and next week's homework.

The functional programming people live for this sort of stuff. ;)


After this material, we'll be continuing into sections 2.2.2 and 2.2.3, extending these "recursive traversal" ideas to include trees.

And I think after that we'll head towards the eight queens problem (a classic; exercise 2.42 in the text) and look at the MC Escher style discussion in 2.2.4. I have a DrRacket library that will let us do those picture transformations, or at least something like them.

https://cs.bennington.college /courses /fall2021 /sicp /notes /09-23
last modified Thu September 23 2021 5:05 pm

attachments [paper clip]

  last modified size
TXT reverse.scm Thu Sep 23 2021 05:05 pm 1.3K