Author Topic: test for dotted pair  (Read 3721 times)

0 Members and 1 Guest are viewing this topic.

CLIMBCUTTER

  • Mosquito
  • Posts: 10
test for dotted pair
« on: October 06, 2012, 06:39:15 PM »
Does anyone know a good test for a dotted pair or cons predicate?

(cdr '("box" . 2)) returns 2, an atom
(cdr '("box" . nil) returns nil, also an atom and a list, and is identical '("box")
(cdr '("box" 2) returns (2) a list

Any ideas?
Jim



Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: test for dotted pair
« Reply #1 on: October 06, 2012, 07:51:57 PM »

Perhaps something like this

Code - Auto/Visual Lisp: [Select]
  1.  ;|--------------------------------------------------------
  2. Function: (KDUB:Dotted-Pair-p arg )
  3. RETURN : T if arg is a dotted-pair list
  4. nil otherwise
  5. Notes:
  6. Anything that is not a list is considered an atom. NIL is an atom.
  7. ----------------------------------------------------------|;
  8.  
  9. (defun kdub:dotted-pair-p (arg) (and (not (atom arg)) (not (listp (cdr arg)))))
  10.  
  11.  
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: test for dotted pair
« Reply #2 on: October 06, 2012, 08:10:40 PM »
Another couple:

Code - Auto/Visual Lisp: [Select]
  1. (defun _dotted-pair-p ( x ) (and (listp x) (not (vl-list-length x))))
Code - Auto/Visual Lisp: [Select]
  1. (defun _dotted-pair-p ( x ) (not (or (atom x) (listp (cdr x)))))

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: test for dotted pair
« Reply #3 on: October 07, 2012, 12:25:02 AM »
One more.
Code: [Select]
(defun _dotted-pair-p ( x ) (and (vl-consp x)(null (vl-list-length x))))
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CLIMBCUTTER

  • Mosquito
  • Posts: 10
Re: test for dotted pair
« Reply #4 on: October 07, 2012, 11:27:40 AM »
Thanks guys,

vl-list-length was what I was missing.

Jim

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: test for dotted pair
« Reply #5 on: October 07, 2012, 12:03:59 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun dottedPairp (p) (and (listp p) (cdr p) (atom (cdr p))))
Speaking English as a French Frog

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: test for dotted pair
« Reply #6 on: October 07, 2012, 06:52:34 PM »

Here are a couple more I use.

Code - Auto/Visual Lisp: [Select]
  1.  ;|--------------------------------------------------------
  2. Function: (KDUB:Dotted-Pair-p arg )
  3. RETURN : T if arg is a dotted-pair list
  4. nil otherwise
  5. Notes:
  6. Anything that is not a list is considered an atom. NIL is an atom.
  7. ----------------------------------------------------------|;
  8.  
  9. (defun kdub:dotted-pair-p (arg) (and (not (atom arg)) (not (listp (cdr arg)))))


Code - Auto/Visual Lisp: [Select]
  1. ;|---------------------------------------------------------
  2. Function: (KDUB:normal-list-p   arg )
  3. RETURN : T if arg is a list
  4. nil if arg is atom or nil or dotted-pair list
  5. EXAMPLE:
  6. (KDUB:normal-list-p nil)       ==>> nil
  7. (KDUB:normal-list-p "abc")    ==>> nil
  8. (KDUB:normal-list-p '(1 . 3 )) ==>> nil
  9. (KDUB:normal-list-p '((1 2 3)(4 5 6)) ) ==>> T
  10. (KDUB:normal-list-p '((1 2 3)(4 . 5)(a b c)) ) =>> T
  11. (KDUB:normal-list-p '((1 . "first")(2 . "second")) ) ==>> T
  12. (KDUB:normal-list-p (car '((1 . "first")(2 . "second")) )) ==>> nil
  13. (KDUB:normal-list-p (car '(("a" "first")(2 . "second")) )) ==>> T
  14. ----------------------------------------------------------|;
  15. (defun kdub:normal-list-p (arg / return)
  16.   (setq return (if (not (atom arg))
  17.                  (vl-list-length arg)
  18.                  nil
  19.                )
  20.   )
  21.   (if (or (not return) (zerop return))
  22.     nil
  23.     t
  24.   )
  25. )

Code - Auto/Visual Lisp: [Select]
  1.  ;|---------------------------------------------------------
  2. Function: (KDUB:string-list-p arg)
  3. RETURN : T if arg is a list of strings
  4. nil otherwise
  5. EXAMPLE:
  6. (KDUB:string-list-p '("A" "B" "C" "D"))  ==>> T
  7. (KDUB:string-list-p '(1 "B" "C" "D"))    ==>> nil
  8. (KDUB:string-list-p '("A" . "Default"))  ==>> nil
  9.  
  10. Requires :
  11.    KDUB:normal-list-p
  12.    KDUB:string-p
  13. ----------------------------------------------------------|;
  14. (defun kdub:string-list-p (arg)
  15.   (and (kdub:normal-list-p arg)
  16.        (apply (function and) (mapcar (function kdub:string-p) arg))
  17.   )
  18. )

Code - Auto/Visual Lisp: [Select]
  1.  ;|--------------------------------------------------------
  2. Function:      (KDUB:number-list-p arg)
  3. RETURN : T if arg is a list of numbers
  4. nil otherwise
  5. EXAMPLE:
  6. (KDUB:number-list-p '(1 "B" "C" "D"))   ==>> nil
  7. (KDUB:number-list-p '(1 2 3.4 ))         ==>> T
  8.  
  9. Requires :
  10.    KDUB:normal-list-p
  11.    KDUB:string-p    
  12. ----------------------------------------------------------|;
  13. (defun kdub:number-list-p (arg)
  14.   (and (kdub:normal-list-p arg)
  15.        (apply (function and) (mapcar (function numberp) arg))
  16.   )
  17. )


Code - Auto/Visual Lisp: [Select]
  1.  
  2.  
  3. ;; (KDUB:emptystring-p "1") ->nil
  4. ;; (KDUB:emptystring-p "") ->T
  5. ;; (KDUB:emptystring-p " ") ->T
  6. ;;
  7. ;; (KDUB:notemptystring-p "x") -> T
  8. ;; (KDUB:notemptystring-p "") -> nil
  9. ;; (KDUB:notemptystring-p " ") -> nil
  10. ;;
  11. ;; (KDUB:validstring-p "x") -> T
  12. ;; (KDUB:validstring-p "") -> nil
  13. ;; (KDUB:validstring-p " ") -> T
  14. (defun kdub:string-p (arg) (= (type arg) 'str))
  15.  
  16. (defun kdub:validstring-p (arg) (and (= (type arg) 'str) (/= 0 (strlen arg))))
  17.  
  18. (defun kdub:emptystring-p (arg)
  19.   (and (= (type arg) 'str) (= 0 (strlen (vl-string-trim " " arg))))
  20. )
  21.  
  22. (defun kdub:notemptystring-p (arg)
  23.   (and (= (type arg) 'str) (/= 0 (strlen (vl-string-trim " " arg))))
  24. )
  25.  
  26. (defun kdub:real-p (arg) (and (equal (type arg) 'real)))
  27.  
  28. (defun kdub:integer-p (arg) (and (equal (type arg) 'int)))
  29.  
  30. (defun kdub:vlaobject-p (arg) (equal (type arg) 'vla-object))
  31.  
  32. (defun kdub:ename-p (arg) (equal (type arg) 'ename))
  33.  
  34. (defun kdub:variant-p (arg) (equal (type arg) 'variant))
  35.  
  36. ;;;
  37. ;;; T if all bitvalues in flag are set, otherwise nil.
  38. (defun kdub:bitsetp (val flag) (= (logand val flag) val))
  39.  
  40.  
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: test for dotted pair
« Reply #7 on: October 07, 2012, 07:32:35 PM »
Another variation of 'normal-list-p':

Code - Auto/Visual Lisp: [Select]
  1. (defun _normal-list-p ( x ) (and x (listp x) (vl-list-length x)))

 :-)

mkweaver

  • Bull Frog
  • Posts: 352
Re: test for dotted pair
« Reply #8 on: October 15, 2012, 02:20:36 PM »
Code: [Select]
(acet-list-is-dotted-pair list)