[plt-scheme] HtDP ex 11.2.4

From: Carl Eastlund (carl.eastlund at gmail.com)
Date: Fri Feb 17 19:08:34 EST 2006

On 2/17/06, Richard Cleis <rcleis at mac.com> wrote:
> I unlearned everything that I knew about programming before starting
> through this book, and it hasn't yet revealed the likes of "list?" or
> "pair?" or "atom?".  Is this problem solvable with only the now
> well-worn "empty?" ?

It is.  It is solvable with the standard design recipe.  Bear in mind
that the standard "cond" part of the template for lists can be written
multiple ways.  You are probably accustomed to:

(define (list-template a-list)
    [(empty? a-list) ...]
    [else ...]))

(Note that that is not a complete template, just part of it.)  Another
completely equivalent way is:

(define (list-template a-list)
    [(empty? a-list) ...]
    [(cons? a-list) ...]))

If you wanted to be creative, you could then switch the order of the
clauses, and then even replace the (empty? a-list) with "else".

My point in all of this is that your familiar "else" clause is not
just a "catch-all", it is the implicit (cons? a-list) clause because
your data definition dictates that if a list is not empty, it is
cons-tructed with cons.  If you look at it that way, a solution
following the same ol' design recipe is probably much clearer.

Carl Eastlund
"Cynical, but technically correct."

Posted on the users mailing list.