CS 3304
Solutions to Homework Assignment 3
July 24, 1997

[12] 1.

Recall that an S-expression is either an atom or a list. Suppose that j is an S-expression. Define the depth of j to be

Some examples follow:

tabular48

Write a Scheme function definition for a function called depth that takes one argument and returns the depth of its argument. You do not have to turn this in electronically, but do test it with the Scheme interpreter.


Here is a straightforward recursive solution:

(define (depth j)
        (cond
                ((not (list? j)) 0)
                ((null? j) 0)
                (else
                        (max (+ 1 (depth (car j))) (depth (cdr j))
                        )
                )
        )
)


[12] 2.

Chapter 7, Problem 10.


The difficulty with this loop is that the assignment to i in the loop body occurs after k is incremented. As a consequence, the assignment is adjusted in the solutions for the first three languages.



cs3304sm class account
Thu Jul 24 12:53:13 EDT 1997