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

Nil as a case key

3 views
Skip to first unread message

Rand Sobriquet

unread,
Oct 5, 2002, 6:47:00 AM10/5/02
to
CLHS has this example for the symbol case:

(dolist (k '(1 2 3 :four #\v () t 'other))
(format t "~S "
(case k ((1 2) 'clause1)
(3 'clause2)
(nil 'no-keys-so-never-seen)
((nil) 'nilslot)
((:four #\v) 'clause4)
((t) 'tslot)
(otherwise 'others))))
>> CLAUSE1 CLAUSE1 CLAUSE2 CLAUSE4 CLAUSE4 NILSLOT TSLOT OTHERS
=> NIL


ACL 6.2 returns the same output for the expression.

Lispworks 4.2.7 has this output:

Warning: () used as key list in CASE (in clause (NIL (QUOTE
NO-KEYS-SO-NEVER-SEEN))).CLAUSE1 CLAUSE1 CLAUSE2 CLAUSE4 CLAUSE4
NO-KEYS-SO-NEVER-SEEN TSLOT OTHERS
NIL

Even though on the surface it seems as though Lispworks is exhibiting
unusual behavior, I'm not sure if the CLHS example is well defined.
There's this one paragraph in CLTL2 (7.6) about case:

"If there is only one key for a clause, then that key may be written
in place of a list of that key, provided that no ambiguity results.
Such a ``singleton key'' may not be nil (which is confusable with (),
a list of no keys), t, otherwise, or a cons."

So, do you think it is correct to say that use of nil alone as a key
is not allowed? In ACL it's use is ignored, and in Lispworks it's use
leads to a warning about undefined behavior.

Erik Naggum

unread,
Oct 5, 2002, 9:23:30 AM10/5/02
to
* Rand Sobriquet

| "If there is only one key for a clause, then that key may be written
| in place of a list of that key, provided that no ambiguity results.
| Such a ``singleton key'' may not be nil (which is confusable with (),
| a list of no keys), t, otherwise, or a cons."

While it says that `nil´ is not allowed as a singleton key, it is clearly
allowed as a list of no keys, since that it explicitly mentioned.

| So, do you think it is correct to say that use of nil alone as a key is
| not allowed? In ACL it's use is ignored, and in Lispworks it's use leads
| to a warning about undefined behavior.

LispWorks is wrong on this one. If you are a supported customer,
complain and you will most probably receive a patch forthwith.

Case keys with `()´ are important because it is often much, much easier
to machine-generate (as in macros) an empty list of keys than to make the
whole clause go away.

--
Erik Naggum, Oslo, Norway

Act from reason, and failure makes you rethink and study harder.
Act from faith, and failure makes you blame someone and push harder.

Lieven Marchand

unread,
Oct 5, 2002, 8:06:56 AM10/5/02
to
rsobr...@eudoramail.com (Rand Sobriquet) writes:

> So, do you think it is correct to say that use of nil alone as a key
> is not allowed? In ACL it's use is ignored, and in Lispworks it's use
> leads to a warning about undefined behavior.

I think it's more a style warning. A compiler is allowed to issue
these on whatever it feels like. Empty key lists are allowed. This is
probably not something a programmer will write manually but could be
the result of macros or of other computed code like (case
((#.+config-parameter-foo) ...)).

--
Hai koe, zei de stier,
Kom mee met mij in de wei,
Dan zijn we tweezaam.
Lieven Marchand <m...@wyrd.be>

Vassil Nikolov

unread,
Oct 5, 2002, 10:02:54 PM10/5/02
to
On 05 Oct 2002 14:06:56 +0200, Lieven Marchand <m...@wyrd.be> said:

LM> I think it's more a style warning. A compiler is allowed to issue
LM> these on whatever it feels like.

And a very easy way to check such a thing is to see if the warning
is issued only during compilation, but not while running compiled
code.

---Vassil.

--
Garbage collection is charged at 0.19e-9 cents a cons. Bulk rates
are also available: please contact memory management for details.

Lieven Marchand

unread,
Oct 6, 2002, 7:47:09 AM10/6/02
to
Vassil Nikolov <vnik...@poboxes.com> writes:

> On 05 Oct 2002 14:06:56 +0200, Lieven Marchand <m...@wyrd.be> said:
>
> LM> I think it's more a style warning. A compiler is allowed to issue
> LM> these on whatever it feels like.
>
> And a very easy way to check such a thing is to see if the warning
> is issued only during compilation, but not while running compiled
> code.

Which is the case. But I think there is a problem with Lispworks CASE.

Consider the following:

CL-USER 12 > (defun foo (x) (case x (a 1) (b 2) (() 3) ((nil) 4)))
Warning: () used as key list in CASE (in clause (NIL 3)).
FOO

CL-USER 13 > (foo 'nil)
3

I would expect 4 as result here.

Vassil Nikolov

unread,
Oct 7, 2002, 12:31:03 AM10/7/02
to
On 06 Oct 2002 13:47:09 +0200, Lieven Marchand <m...@wyrd.be> said:

[...]
LM> there is a problem with Lispworks CASE.

LM> Consider the following:

LM> CL-USER 12 > (defun foo (x) (case x (a 1) (b 2) (() 3) ((nil) 4)))
LM> Warning: () used as key list in CASE (in clause (NIL 3)).
LM> FOO

LM> CL-USER 13 > (foo 'nil)
LM> 3

LM> I would expect 4 as result here.

Certainly. So the warning wasn't there for nothing...

Tim Bradshaw

unread,
Oct 7, 2002, 12:25:26 PM10/7/02
to
> I would expect 4 as result here.

But you'd be wrong! Think of it as (case ... (() x) ...).

--tim

Lieven Marchand

unread,
Oct 8, 2002, 12:50:34 PM10/8/02
to
tfb+g...@tfeb.org (Tim Bradshaw) writes:

> > I would expect 4 as result here.
>
> But you'd be wrong! Think of it as (case ... (() x) ...).

Examples in the standard aren't normative, but the Hyperspec seems to
disagree with you.

(dolist (k '(1 2 3 :four #\v () t 'other))
(format t "~S "
(case k ((1 2) 'clause1)
(3 'clause2)
(nil 'no-keys-so-never-seen)
((nil) 'nilslot)
((:four #\v) 'clause4)
((t) 'tslot)
(otherwise 'others))))
>> CLAUSE1 CLAUSE1 CLAUSE2 CLAUSE4 CLAUSE4 NILSLOT TSLOT OTHERS
=> NIL

--

Erik Naggum

unread,
Oct 8, 2002, 6:42:53 PM10/8/02
to
* Lieven Marchand <m...@wyrd.be>

| Examples in the standard aren't normative, but the Hyperspec seems to
| disagree with you.

Would you please pay attention and stop spreading disinformation?

Tim Bradshaw

unread,
Oct 9, 2002, 4:24:10 AM10/9/02
to
>
> Examples in the standard aren't normative, but the Hyperspec seems to
> disagree with you.

I'm now confused, and I may have said something wrong earlier, in
which case sorry! I think that in the case of (case ... (nil ...)
...) that clause will never match. If you want to match NIL you need
to say (case ... ((nil) ...) ...).

But maybe I'm wrong.

--tim

Tim Bradshaw

unread,
Oct 10, 2002, 4:22:39 AM10/10/02
to
> I'm now confused, and I may have said something wrong earlier, in
> which case sorry! I think that in the case of (case ... (nil ...)
> ...) that clause will never match. If you want to match NIL you need
> to say (case ... ((nil) ...) ...).

I have now checked and (a) I was wrong (because I didn't check the
code that was posted, sorry); (b) LW seems to have a fairly nasty bug
in CASE.

So, sorry for claiming stuff that was wrong.

--tim

0 new messages