Author Topic: FYI - listp function  (Read 1359 times)

0 Members and 1 Guest are viewing this topic.

Tharwat

  • Swamp Rat
  • Posts: 707
  • Hypersensitive
FYI - listp function
« 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

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: FYI - listp function
« Reply #1 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
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Tharwat

  • Swamp Rat
  • Posts: 707
  • Hypersensitive
Re: FYI - listp function
« Reply #2 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.

dexus

  • Newt
  • Posts: 196
Re: FYI - listp function
« Reply #3 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.

Tharwat

  • Swamp Rat
  • Posts: 707
  • Hypersensitive
Re: FYI - listp function
« Reply #4 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.  

domenicomaria

  • Swamp Rat
  • Posts: 723
Re: FYI - listp function
« Reply #5 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

dexus

  • Newt
  • Posts: 196
Re: FYI - listp function
« Reply #6 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.
« Last Edit: July 25, 2022, 07:39:24 AM by dexus »

Tharwat

  • Swamp Rat
  • Posts: 707
  • Hypersensitive
Re: FYI - listp function
« Reply #7 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

dexus

  • Newt
  • Posts: 196
Re: FYI - listp function
« Reply #8 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:

JohnK

  • Administrator
  • Seagull
  • Posts: 10604
Re: FYI - listp function
« Reply #9 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
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

mhupp

  • Bull Frog
  • Posts: 250
Re: FYI - listp function
« Reply #10 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)))

kirby

  • Newt
  • Posts: 127
Re: FYI - listp function
« Reply #11 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.  
« Last Edit: July 25, 2022, 03:06:20 PM by kirby »

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: FYI - listp function
« Reply #12 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