CS 3304
Solutions to Homework Assignment 3
July 24, 1997
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
j is an atom, including (); orj is a non-empty list.
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))
)
)
)
)
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.