Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

atom? in scheme

0 views
Skip to first unread message

Terrence Brannon

unread,
Dec 4, 2003, 9:44:12 AM12/4/03
to
I am reading "The Little LISPer" but thought I would use Scheme instead
of Lisp because Scheme is the more lightweight of the two languages.

How do I check if something is atomic in scheme?

I am using Bigloo scheme. It is easy to compile on Cygwin and has a nice
manual.

What scheme do you use?

Toni Nikkanen

unread,
Dec 4, 2003, 9:56:36 AM12/4/03
to
Terrence Brannon <meta...@urth.org> writes:

> How do I check if something is atomic in scheme?

It is an atom if it is not nil and it is not a pair.

Bradd W. Szonye

unread,
Dec 4, 2003, 10:09:00 AM12/4/03
to
Terrence Brannon <meta...@urth.org> wrote:
> I am reading "The Little LISPer" but thought I would use Scheme
> instead of Lisp because Scheme is the more lightweight of the two
> languages.
>
> How do I check if something is atomic in scheme?

You check whether it belongs to one of the atomic types:

(or (boolean? x)
(symbol? x)
(char? x)
(procedure? x)
(number? x)
(port? x)
(null? x))

or you make sure that it doesn't belong to a non-atomic type:

(not (or (pair? x) (vector? x) (string? x)))

Note that I've classified strings as "non-atomic," since "objects such
as pairs, vectors, and strings implicitly denote locations or sequences
of locations." You may want to consider them "atomic," depending on what
you're trying to do. (Strings containment isn't a fully general,
"closed" operation like pair and vector containment is, but strings
aren't really atomic either.)

Here's another way to check a list of predicates:

(define (check val . preds)
(let loop ((p preds))
(cond ((null? p) #f)
(((car p) val) #t)
(else (loop (cdr p))))))

Using this, you can write ATOM? as

(define (atom? x)
(check x boolean? symbol? char? procedure? number? port? null?))

If you do a lot of predicate testing like this, it may eventually save
you some typing.

> I am using Bigloo scheme. It is easy to compile on Cygwin and has a
> nice manual. What scheme do you use?

I use PLT Scheme (MzScheme). I originally chose it because it works on
both Windows and Linux, with a common GUI library for both systems. I
played around with some other Schemes first, but I couldn't find any
others that were suitable for the project I was working on at the time.
I've put that project on hold, but I've grown accustomed to PLT in the
meantime, so there's currently no real reason for me to switch. Lately
I've been toying with the idea of writing my own implementation (a
native compiler for i386 Linux systems).
--
Bradd W. Szonye
http://www.szonye.com/bradd

Bradd W. Szonye

unread,
Dec 4, 2003, 10:11:59 AM12/4/03
to
> Terrence Brannon <meta...@urth.org> writes:
>> How do I check if something is atomic in scheme?

Toni Nikkanen <to...@tuug.fi> wrote:
> It is an atom if it is not nil and it is not a pair.

Don't forget vectors and strings. Neither are atomic. (Strings aren't
closed over containment, but they aren't atomic either.)

Also, I would personally think of the empty list as a atom.

Abdulaziz Ghuloum

unread,
Dec 4, 2003, 10:28:53 AM12/4/03
to
Bradd W. Szonye wrote:
> Terrence Brannon <meta...@urth.org> wrote:
>
>>I am reading "The Little LISPer" but thought I would use Scheme
>>instead of Lisp because Scheme is the more lightweight of the two
>>languages.
>>
>>How do I check if something is atomic in scheme?
>
>
> You check whether it belongs to one of the atomic types:
>
> (or (boolean? x)
> (symbol? x)
> (char? x)
> (procedure? x)
> (number? x)
> (port? x)
> (null? x))
>

From the preface (page xii):

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


The next line says

"try (atom? (quote ())) and make sure it returns #f"


Aziz,,,

Bradd W. Szonye

unread,
Dec 4, 2003, 11:10:18 AM12/4/03
to
>> Terrence Brannon <meta...@urth.org> wrote:
>>> How do I check if something is atomic in scheme?

> Bradd W. Szonye wrote:
>> You check whether it belongs to one of the atomic types:
>>
>> (or (boolean? x) (symbol? x) (char? x) (procedure? x) (number? x)
>> (port? x) (null? x))

Abdulaziz Ghuloum <aghu...@cs.indiana.edu> wrote:
> From the preface (page xii):
>
> (define atom?
> (lambda (x)
> (and (not (pair? x)) (not (null? x)))))
>
> The next line says
>
> "try (atom? (quote ())) and make sure it returns #f"

That may well be how you define an "atom" in The Little Schemer, but
it's not sufficient for defining atoms in Scheme, if by "atom" you mean
"an object that doesn't contain other objects." Vectors are clearly
non-atomic, and strings are arguably non-atomic also.

Meanwhile, I'm not sure whether it's a good idea to consider the empty
list "non-atomic." What are the reasons for doing so?

Richard C. Cobbe

unread,
Dec 4, 2003, 11:17:17 AM12/4/03
to
Abdulaziz Ghuloum <aghu...@cs.indiana.edu> writes:

> Bradd W. Szonye wrote:
>> Terrence Brannon <meta...@urth.org> wrote:
>>
>>>I am reading "The Little LISPer" but thought I would use Scheme
>>>instead of Lisp because Scheme is the more lightweight of the two
>>>languages.
>>>
>>>How do I check if something is atomic in scheme?
>> You check whether it belongs to one of the atomic types:
>> (or (boolean? x)
>> (symbol? x)
>> (char? x)
>> (procedure? x)
>> (number? x)
>> (port? x)
>> (null? x))

Generally, yes. For the purposes of The Little LISPer/Schemer,
(and (not? null x) (not (pair? x)))
is sufficient: the only compound data type introduced in the book is the
list.

> From the preface (page xii):
>
> (define atom?
> (lambda (x)
> (and (not (pair? x)) (not (null? x)))))

I was going to respond with that, but I wasn't sure which edition of the
book the OP was using. The fact that he gives the title as _The Little
LISPer_ indicates that he's not using 4ed.

This is significant because the book's definition of atom? changed between
3ed and 4ed: IIRC, originally (atom? '()) ==> #t.

Richard


Bradd W. Szonye

unread,
Dec 4, 2003, 11:34:40 AM12/4/03
to
Richard C. Cobbe <co...@ccs.neu.edu> wrote:
> This is significant because the book's definition of atom? changed
> between 3ed and 4ed: IIRC, originally (atom? '()) ==> #t.

Do you know why that changed? It seems like the better definition to me,
at least going by gut feel. I haven't finished reading The Little
Schemer yet, so my apologies if it's explained therein.

Richard C. Cobbe

unread,
Dec 4, 2003, 2:07:36 PM12/4/03
to
"Bradd W. Szonye" <bradd...@szonye.com> writes:

> Richard C. Cobbe <co...@ccs.neu.edu> wrote:
>> This is significant because the book's definition of atom? changed
>> between 3ed and 4ed: IIRC, originally (atom? '()) ==> #t.
>
> Do you know why that changed? It seems like the better definition to me,
> at least going by gut feel. I haven't finished reading The Little
> Schemer yet, so my apologies if it's explained therein.

No, it's not explained in the books.

As Matthias Felleisen just explained to me, this changed because R5RS came
out between TLL3 and TLS4, and R5RS was of course the first RnRS that
required '() and #f to be different. The authors brought this distinction
into the books.

The immediate cause, as I recall, was a bug I found in ch 19 of _The
Seasoned Schemer_, just before it went to press. This was long enough ago
that I don't remember the details, but in investigating the bug, Matthias &
Dan recognized that Scheme's distinction between '() and #f was the Right
Thing, so they shifted.

Richard

0 new messages