[plt-scheme] HTDP: 14.3.4 Retaining Values

From: Geoffrey Lane (geofflane at gmail.com)
Date: Sat Sep 27 12:13:24 EDT 2008

I hope this is ok, it's my first post to the group.

14.3.4 Basically counts the max depth of nested lists (what are called
Web Pages in the example)
'(foo) is 0 - no nesting
'(foo (bar)
        (quux (zorch))) is 3 for the maximum depth.

It's supposed to just take the Web Page (list of lists) - but I could
only figure it out when I passed an initial zero that I could then
increment in the proper condition (when I'm going down a level).
Am I missing something? The reason I ask, is there's a question in 15
that wants to do a similar thing.

Code I came up with:

;; depth: WP  ->  number
;; to count the depth of sub-pages that occur in a-wp
(define (depth a-wp n)
(count-depth a-wp 0))

;; count-depth: WP number -> number
;; count the depth of the sub-pages of a-wp
(define (count-depth a-wp n)
   [(empty? a-wp) n]
   [(symbol? (first a-wp)) (count-depth (rest a-wp) n)]
   [else (max (count-depth (first a-wp) (add1 n)) (count-depth (rest a-
wp) n))]))

Thanks for the help.

Geoff Lane <geofflane at gmail.com>

Posted on the users mailing list.