The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Message from discussion problem with lat? (TLS 4ed)

From:
To:
Cc:
Followup To:
Subject:
 Validation: For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon.

More options May 29 2012, 9:30 am
Newsgroups: comp.lang.scheme
From: Barry Margolin <bar...@alum.mit.edu>
Date: Tue, 29 May 2012 09:30:07 -0400
Local: Tues, May 29 2012 9:30 am
Subject: Re: problem with lat? (TLS 4ed)
colin howarth <co...@howarth.de> wrote:

> Hi, I'm just starting out with Scheme (and Lisp) and am going through The
> Little Schemer.

> I'm confused, right at the beginning and wanted to ask, since this seems
> important (cf. The First Commandment)...

> lat? is defined in the book as

> (define lat?
>   (lambda (l)
>     (cond
>       ((null? l) #t)
>       ((atom? (car l)) (lat? (cdr l)))
>       (else #f))))

> This terminates with #t when l has become the empty list (and all its
> elements were atoms).

> However, it also evaluates to #t if given the empty list to start with.

> But atom? is defined as

> (define atom?
>   (lambda (x)
>     (and
>       (not (pair? x))
>       (not (null? x)))))

> which says that x must not be null.

> My attempt at lat? was

> (define lat?
>   (lambda (l)
>     (and
>       ((atom? (car l))
>       (or
>         (null? (cdr l))
>         (lat? (cdr l))))))

> which reads as "the first element is an atom and the rest is null, or a list
> of atoms". That works, except when given the empty list, where (car l) batfs.
> Which isn't much better than returning #t :-)

In a recursive algorithm, you have to handle the case where the
recursion bottoms out *before* doing the non-terminal processes.  You're
doing them in the wrong order.

> [I've done a couple of days of Lisp where (car ()) => nil. In Scheme I don't
> even know how to *type* an empty list yet, so I'm using (cdr '(a)) or (lambda
> () ())]

You can type it as '().

> So. If you've got this far, I suppose the question is should lat? return #t
> for an empty list?

The following should be true for any l1 and l2:

(eqv? (lat? (append l1 l2))
(and (lat? l1) (lat? l2)))

What if l1 or l2 is the empty list, how can you ensure the tautology?

This type of equation is how you generally figure out how to handle
limiting cases like this.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***