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

how to check if a list is empty or nil?

11,852 views
Skip to first unread message

ssecorp

unread,
Aug 23, 2008, 11:59:31 AM8/23/08
to
(not (= lst nil))

(/= lst nil)

empty?, list?, nil? does not work either.

do I really have to use (= 0 (length lst))
seems inefficient if the lists can be big.

Brian

unread,
Aug 23, 2008, 12:07:50 PM8/23/08
to
Use NULL. ENDP also works.

Kenny

unread,
Aug 23, 2008, 12:19:11 PM8/23/08
to

lst. Or list if you realize you are not doing scheme.

kt

ssecorp

unread,
Aug 23, 2008, 12:42:14 PM8/23/08
to

huh?

Barry Margolin

unread,
Aug 23, 2008, 1:02:25 PM8/23/08
to
In article
<8ef28f1e-2d71-482d...@m73g2000hsh.googlegroups.com>,
ssecorp <circul...@gmail.com> wrote:

> (not (= lst nil))
>
> (/= lst nil)

= and /= are for numbers. EQ is the predicate you should use if you
want to compare with NIL.

>
> empty?, list?, nil? does not work either.

Others have mentioned NULL. There's also NOT (although that should
generally only be used when you're treating it as a boolean, rather than
a list) and ENDP (which is generally used when you're iterating, hence
the name).

>
> do I really have to use (= 0 (length lst))
> seems inefficient if the lists can be big.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***

DeverLite

unread,
Aug 23, 2008, 2:06:02 PM8/23/08
to

I think Kenny's point is that you can use the name "list" rather than
"lst", and this will not conflict with the function (named "list")
because, unlike scheme, CL has a separate namespace for functions.

Pascal J. Bourguignon

unread,
Aug 23, 2008, 2:51:05 PM8/23/08
to
DeverLite <derby....@gmail.com> writes:

No. Kenny's point is that to test for a not null list you can just
write list. In the same way that in C, to test for a non zero integer
you can just write integer.


And the OP should try reading some initiation book about Common Lisp,
like:

Common Lisp: A Gentle Introduction to Symbolic Computation
http://www-cgi.cs.cmu.edu/afs/cs.cmu.edu/user/dst/www/LispBook/index.html
http://www.cs.cmu.edu/~dst/LispBook/


--
__Pascal Bourguignon__ http://www.informatimago.com/
Kitty like plastic.
Confuses for litter box.
Don't leave tarp around.

Chris Russell

unread,
Aug 23, 2008, 2:38:56 PM8/23/08
to

The empty list is false so you can write

(if list
(do stuff);non-empty case
(do other stuff));empty case

Otherwise do what Barry says.

Kenny

unread,
Aug 23, 2008, 3:14:48 PM8/23/08
to

1. nil is false
2. there is no need to mangle the name
3. 1 and 2 are false in Scheme

kt

0 new messages