how to check if a list is empty or nil?

This is a discussion on how to check if a list is empty or nil? within the lisp forums in Programming Languages category; (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....

Go Back   Application Development Forum > Programming Languages > lisp

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-23-2008, 11:59 AM
ssecorp
Guest
 
Default how to check if a list is empty or nil?

(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.
Reply With Quote
  #2  
Old 08-23-2008, 12:07 PM
Brian
Guest
 
Default Re: how to check if a list is empty or nil?

On Aug 23, 10:59*am, ssecorp <circularf...@gmail.com> wrote:
> (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.

Use NULL. ENDP also works.
Reply With Quote
  #3  
Old 08-23-2008, 12:19 PM
Kenny
Guest
 
Default Re: how to check if a list is empty or nil?

ssecorp wrote:
> (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.


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

kt
Reply With Quote
  #4  
Old 08-23-2008, 12:42 PM
ssecorp
Guest
 
Default Re: how to check if a list is empty or nil?

On Aug 23, 6:19*pm, Kenny <kentil...@gmail.com> wrote:
> ssecorp wrote:
> > (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.

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


huh?
Reply With Quote
  #5  
Old 08-23-2008, 01:02 PM
Barry Margolin
Guest
 
Default Re: how to check if a list is empty or nil?

In article
<8ef28f1e-2d71-482d-9e6c-10c18407a5b8@m73g2000hsh.googlegroups.com>,
ssecorp <circularfunc@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, barmar@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 ***
Reply With Quote
  #6  
Old 08-23-2008, 02:06 PM
DeverLite
Guest
 
Default Re: how to check if a list is empty or nil?

On Aug 23, 11:42*am, ssecorp <circularf...@gmail.com> wrote:
> On Aug 23, 6:19*pm, Kenny <kentil...@gmail.com> wrote:
>
> > ssecorp wrote:
> > > (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.

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

>
> > kt

>
> huh?


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.
Reply With Quote
  #7  
Old 08-23-2008, 02:38 PM
Chris Russell
Guest
 
Default Re: how to check if a list is empty or nil?

On 23 Aug, 16:59, ssecorp <circularf...@gmail.com> wrote:
> (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.


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.
Reply With Quote
  #8  
Old 08-23-2008, 02:51 PM
Pascal J. Bourguignon
Guest
 
Default Re: how to check if a list is empty or nil?

DeverLite <derby.little@gmail.com> writes:

> On Aug 23, 11:42*am, ssecorp <circularf...@gmail.com> wrote:
>> On Aug 23, 6:19*pm, Kenny <kentil...@gmail.com> wrote:
>>
>> > ssecorp wrote:
>> > > (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.

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

>>
>> > kt

>>
>> huh?

>
> 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.


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...ook/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.
Reply With Quote
  #9  
Old 08-23-2008, 03:14 PM
Kenny
Guest
 
Default Re: how to check if a list is empty or nil?

ssecorp wrote:
> On Aug 23, 6:19 pm, Kenny <kentil...@gmail.com> wrote:
>
>>ssecorp wrote:
>>
>>>(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.

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

>
>
> huh?


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

kt
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 02:11 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.