[plt-scheme] HtDP ex 11.2.4
On Feb 17, 2006, at 2:24 AM, Richard Cleis wrote:
> I unlearned everything that I knew about programming before starting
> through this book,
That's good :-)
> and it hasn't yet revealed the likes of "list?" or "pair?" or "atom?".
pair? = cons?
In HtDP Scheme, you know that the cdr (rest) of a cons is a cons. So
(list? l) is just (or (empty? l) (cons? l)).
> Is this problem solvable with only the now well-worn "empty?"
Here is what you get when you follow the design recipe:
Data Def:
DL is one of:
-- symbol
-- (cons DL empty)
Some data examples: 'fun, (cons 'fun empty), (cons (cons 'fun empty)
empty)
The template is:
(define (f-for-dl one-dl)
(cond
[(symbol? one-dl) ...]
[else ... (f-for-dl (first one-dl)) ... ])) ;; (rest one-dl) is
always empty according to DD.
Examples:
(deep 'fun) = 0
(deep (cons 'fun empty)) = 1
(deep (cons (cons 'funny empty) empty) = 2
Function:
(define (deep one-dl)
(cond
[(symbol? one-dl) 0]
[else (+ (deep (first one-dl)) 1)]))
Now test. -- Matthias
deep-list is either
s where s is some symbol or
(cons dl empty), where dl is a deep list.