Dotted-pair detection

Hello,
in AutoLisp: how to test whether a list is of dotted-pair type?
Thank you in advance. Best regards -
Honza

Comments

  • It depends on the definition of 'dotted pair':
    ; (KGA_List_DottedPair_P '(0 . 1)) => T ; (KGA_List_DottedPair_P '((0 1 2 3) . 0)) => T ; (KGA_List_DottedPair_P '(1 2 . 3)) => nil ; (KGA_List_DottedPair_P '(nil . 1)) => T (defun KGA_List_DottedPair_P (anyLst) (not (listp (cdr anyLst))) ) ; (KGA_List_DottedPairSimple_P '(0 . 1)) => T ; (KGA_List_DottedPairSimple_P '((0 1 2 3) . 0)) => nil ; (KGA_List_DottedPairSimple_P '(1 2 . 3)) => nil ; (KGA_List_DottedPairSimple_P '(nil . 1)) => nil (defun KGA_List_DottedPairSimple_P (anyLst) (and (not (listp (car anyLst))) (not (listp (cdr anyLst))) ) )

  • Different spin on an old post ...

    (defun mpx-dotted-pair-p ( x ) 
        (and
            (listp x)
            (not (vl-list-length x))
        )
    )
    

    Test it:

    (foreach x
    
       '(
            (0 . 1) 
            ((0 1 2 3) . 0)
            (1 2 . 3)
            (nil . 1)
            (10 0.0 0.0 0.0)
            (tate 42)
            42
            nil
        )
    
        (princ 
            (strcat 
                "\n(mpx-dotted-pair-p "
                (vl-prin1-to-string x) 
                ") >> "
                (if (mpx-dotted-pair-p x) "t" "nil")
            )
        )
    
        (princ)    
    
    )
    

    Output:

    (mpx-dotted-pair-p (0 . 1))          >> t
    (mpx-dotted-pair-p ((0 1 2 3) . 0))  >> t
    (mpx-dotted-pair-p (1 2 . 3))        >> t
    (mpx-dotted-pair-p (NIL . 1))        >> t
    (mpx-dotted-pair-p (10 0.0 0.0 0.0)) >> nil
    (mpx-dotted-pair-p (TATE 42))        >> nil
    (mpx-dotted-pair-p 42)               >> nil
    (mpx-dotted-pair-p nil)              >> nil
    
    

    Cheers.

    Michael.

This discussion has been closed.