[plt-scheme] HTDP: 14.3.4 Retaining Values

From: Geoffrey Lane (geofflane at gmail.com)
Date: Sat Sep 27 19:08:47 EDT 2008

On Sep 27, 2008, at 11:27 AM, Matthias Felleisen wrote:

> 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))))

I ended up with something slightly different - but it works:

(max (+ 1 (depth (first a-wp)))
                     (depth (rest a-wp)))

My current understanding is that (+ 1 (max  (depth ... ends up  
counting all of the sub-lists in the top-level list.
(which is the same as if no max was used)

While (max (+ 1 (depth finds the  maximum depth of any sub-list -  
which is what I think I was looking for.

Thanks for the explanation though, it put me back on the right track  
(until/unless you tell me this is wrong somehow :-)).

Geoff Lane <geofflane at gmail.com>

Posted on the users mailing list.