TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Tharwat on July 24, 2022, 07:29:53 AM

Title: FYI - listp function
Post by: Tharwat on July 24, 2022, 07:29:53 AM
Have you ever come across is that the function LISTP would return T with nil ?

I have pulled my hair out until I tested this out and I was sure it will return nil but it did not. :2funny:

So I thought to save people's hair once they encounter such a situation. lol
Title: Re: FYI - listp function
Post by: ribarm on July 24, 2022, 08:08:17 AM
In documentation, it states that nil represent null list...
Watching others with preparations of empty variables for lists, I figured out long time ago that this was not needed and it's only for visual understanding of what preset does...
Example :
(setq lst '())

IMHO, you can always remove this line and it's obvious that :
(eq lst nil) => T
Title: Re: FYI - listp function
Post by: Tharwat on July 24, 2022, 08:16:48 AM
I am talking about LISTP function and not about nil, besides that its only FYI so no solution needed.
Title: Re: FYI - listp function
Post by: dexus on July 25, 2022, 03:18:25 AM
It's annoying that is does, if you want to know if something is a list you can't use the listp function because of this exception.

As an alternative vl-list-length does return true (an int) if it is a list an nil if it's not.
So I think it works better for checking lists than the designated listp function.
Title: Re: FYI - listp function
Post by: Tharwat on July 25, 2022, 05:25:01 AM
I do prefer to stay in vanilla DXF so the following would be enough .
Code - Auto/Visual Lisp: [Select]
  1. (= (type pt) 'LIST)
  2.  
Title: Re: FYI - listp function
Post by: domenicomaria on July 25, 2022, 05:29:37 AM
@dexus
"As an alternative vl-list-length does return true (an int) if it is a list an nil if it's not."

(vl-list-length (list 1 2 3 4) )
4
(vl-list-length nil)
0
Title: Re: FYI - listp function
Post by: dexus on July 25, 2022, 07:21:00 AM
@dexus
"As an alternative vl-list-length does return true (an int) if it is a list an nil if it's not."

(vl-list-length (list 1 2 3 4) )
4
(vl-list-length nil)
0
Oops, you are right.

Guess I should add something for that like this:
Code - Auto/Visual Lisp: [Select]
  1. (and (listp lst) (/= (vl-list-length lst) 0))

But then just a type check like Tharwat proposes is much easier.

Edit, correction in the code.
Title: Re: FYI - listp function
Post by: Tharwat on July 25, 2022, 07:26:20 AM
Code - Auto/Visual Lisp: [Select]
  1. (and (listp lst) (vl-list-length lst))

But then just a type check like Tharwat proposes is much easier.

 ;-)
Code - Auto/Visual Lisp: [Select]
  1. _$ (and (listp nil) (vl-list-length nil))
  2. T
Title: Re: FYI - listp function
Post by: dexus on July 25, 2022, 07:40:21 AM
Code - Auto/Visual Lisp: [Select]
  1. (and (listp lst) (vl-list-length lst))

But then just a type check like Tharwat proposes is much easier.

 ;-)
Code - Auto/Visual Lisp: [Select]
  1. _$ (and (listp nil) (vl-list-length nil))
  2. T
Alright, I'm going to get another cup of coffee.
Thanks for being more awake then me :angel:
Title: Re: FYI - listp function
Post by: JohnK on July 25, 2022, 09:55:20 AM
It is also annoying that LISTP also returns T for association lists as well (but you cannot use mapcar on those).

Code: [Select]
Command: (setq lst '(1 . 2))
(1 . 2)

Command: (listp lst)
T

Command: (mapcar 'princ lst)
1; error: bad list: 2
Title: Re: FYI - listp function
Post by: mhupp on July 25, 2022, 12:16:28 PM
I use this for my selection sets. returns only entity names.

Code - Auto/Visual Lisp: [Select]
  1. (foreach ent (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
Title: Re: FYI - listp function
Post by: kirby on July 25, 2022, 02:51:37 PM
Here is a related function by Gilles Chanteau...
(and a link to related discussion by many of the forum 'heavies')

Code - Auto/Visual Lisp: [Select]
  1. (defun dottedPairp (p)
  2. ; Test for dotted pair, by Gile (Gilles Chanteau)
  3. ; http://www.theswamp.org/index.php?topic=42940.msg482148#msg482148
  4. ; Returns T is dotted pair and nil if not
  5.         (and (listp p) (cdr p) (atom (cdr p)))
  6. )
  7.  
Title: Re: FYI - listp function
Post by: Marc'Antonio Alessi on July 30, 2022, 11:43:49 AM
Code: [Select]
(defun ALE_ListP (x)
  (and x (listp x))
)

(ALE_ListP      nil) => nil
(ALE_ListP      '()) => nil
(ALE_ListP     '(1)) => T
(ALE_ListP '(1 . 0)) => T