Structure and
Interpretation
of Computer
Programs

Fall 2021
course
site
-->

Thu Sep 30

Discuss the homework problems :

And let's do 2.23 in class ... which I meant to assign. ;)

Implement for-each which loops over a list :

  (for-each
    (λ (x) (newline) (display x))
    (list 57 321 88))

Here's my answer .

trees

This week I'd like to continue on in chapter 2, looking at the material related to trees and traversing through trees. Some of this should be familiar to those of you who have seen some data structures already.

I've posted the next set of homework problems. This material continues the fold-left and fold-right ideas that I presented last time, as well as a looking at trees.

To get a start on that stuff, let's go over the material in 2.2.2 : "hierarchical structures" :

Figure 2.5 :

(cons (list 1 2) (list 3 4))

 [ /   \  ]
  /     \
 /       \
[1  2     [3 4]

Figure 2.6

   '((1 2) 3  4)

      1234----
      /   \   \
     12    3   4
    /  \
   1    2

Scheme has a built-in pair? predicate, which we can use to see if we've reached the end points ("leaves") of a tree.

> (pair? 1)
#f
> (pair? (cons 1 2))
#t

The book defines a (count-leaves tree) procedure to see how many terminal nodes there are in a tree. Can we work that out ourselves?

on Monday

... we'll work through exercise 2.42 in class, the classic eight queens puzzle ;)

https://cs.bennington.college /courses /fall2021 /sicp /notes /09-30
last modified Thu September 30 2021 7:43 pm

attachments [paper clip]

  last modified size
TXT 2_23.scm Thu Sep 30 2021 07:43 pm 339B
TXT count.scm Thu Sep 30 2021 07:43 pm 968B