[plt-scheme] HTDP: 14.3.4 Retaining Values
Yes, you are missing one of two key aspect of the design recipe for
this stage of learning. Once you have designed the template for a
function, or a bunch of functions for the same input type, you must
ask and answer the following three questions:
1. What is the result for those cond lines that don't refer back to
the original function? Typically, your examples tell you the answer
here.
In your example: 0
2. What do the expressions in the other (complex) cases compute? Here
a well-articulated purpose statement tells you almost instantaneously
what you want.
In your example: (depth (first wp)) computes the depth of the first
web page on the list, and (depth-list (rest wp)) computes the maximal
depth of the remaining items on the list.
3. How do you have to combine the values of these expressions to get
the result for the actually given input (wp on your example)? This is
the most creative step and the focus of the entire design recipe
approach. I have usually recommended to work through any of the
examples you have developed first. One of my TAs has come up with an
improvement of this suggestion: line up things in a table.
In your example:
wp: '(foo (bar) (baz) (quix (zorch)))
desired result: 3
(first wp): 'foo
(depth (first wp)): 0 ;; your first example says so
(rest wp): '((bar) (baz) (quix (zorch))
(depth-list (rest: 2
Make additional examples. What you will see is that you want (+ 1
(max (depth (first wp)) (depth-list (rest wp))))
-- Matthias
On Sep 27, 2008, at 12:13 PM, Geoffrey Lane wrote:
> 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)
> (baz)
> (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)
> (cond
> [(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>
>
>
>
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme