Author Topic: [challenge] 39 : Testing for proper lists  (Read 326 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
[challenge] 39 : Testing for proper lists
« on: August 01, 2022, 11:04:42 AM »
In Lisp, lists come in several different flavors; "proper lists" and "association lists (dotted pairs)".

DEMONSTRATION:
Code - Auto/Visual Lisp: [Select]
  1. ;; Setup
  2. (setq plst '(1 2 3))
  3. (setq alst '(1 . 2))
  4.  
  5. ;; Checking
  6. ;; -If you run the following code you will get T(rue) for each list.
  7. (listp plst)
  8. (listp alst)
  9.  
  10. ;; Execution
  11. ;; -MAPCAR takes--as it's arguments--a function name and a list but
  12. ;;  if you run the following code you will get an error for the `alst`.
  13. (mapcar 'print plst)
  14. (mapcar 'print alst)

I have been told the a simple definition for a "proper list", in the Lisp languages, is the list is considered `proper` if the list's last CDR returns nil.

Define a function which will check if a list is `proper`.

EXAMPLE:
(properp '(1 2 3 4 5 6 7))
> T
(properp '(1 . 2))
> nil
(properp nil)
> nil
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

ribarm

  • Gator
  • Posts: 2747
  • Marko Ribar, architect
Re: [challenge] 39 : Testing for proper lists
« Reply #1 on: August 01, 2022, 12:01:28 PM »
Who knows...
Just simple thought...

Code: [Select]
(vl-every (function atom) lst)

EDIT :
Still untested...

Code: [Select]
(defun chkproplst ( masterlst )
  (vl-every
    (function (lambda ( lst )
      (not (vl-catch-all-error-p (vl-catch-all-apply
        (function (lambda nil
          (while (and (car lst) (cons (car lst) (setq lst (cdr lst))))
            (and
              (vl-every (function atom) lst)
              (cadr lst)
            )
          )
        ))
        (list lst)
      )))
    ))
    (list masterlst)
  )
)

Code: [Select]
(defun chkproplst ( lst )
  (while (and (car lst) (cons (car lst) (setq lst (cdr lst))))
    (and
      (listp lst)
      (not (vl-catch-all-error-p (vl-catch-all-apply (function cadr) lst)))
    )
  )
)
« Last Edit: August 02, 2022, 12:31:34 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
Re: [challenge] 39 : Testing for proper lists
« Reply #2 on: August 01, 2022, 01:02:39 PM »
Hello ribarm,
ATOM will return T(rue) for the NIL symbol.

(vl-every (function atom) nil)
> T
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

gile

  • Water Moccasin
  • Posts: 2392
  • Marseille, France
Re: [challenge] 39 : Testing for proper lists
« Reply #3 on: August 01, 2022, 02:35:43 PM »
Hi,
My 2 cents.
Code - Auto/Visual Lisp: [Select]
  1. (defun properlistp (l)
  2.   (cond
  3.     ((null (cdr l)) T)
  4.     ((atom (cdr l)) nil)
  5.     (T (properlistp (cdr l)))
  6.   )
  7. )

Code: [Select]
_$ (properlistp '())
T
_$ (properlistp '(1))
T
_$ (properlistp '(1 . 2))
nil
_$ (properlistp '(1 2 3 . 4))
nil
_$ (properlistp '(1 2 (3 . 4)))
T
_$ (properlistp '(1 2 (3 4)))
T
Speaking English as a French Frog

gile

  • Water Moccasin
  • Posts: 2392
  • Marseille, France
Re: [challenge] 39 : Testing for proper lists
« Reply #4 on: August 01, 2022, 02:40:24 PM »
Could have been written:
Code - Auto/Visual Lisp: [Select]
  1. (defun properlistp (l)
  2.   (or (null (cdr l))
  3.       (and (not (atom (cdr l)))
  4.            (properlistp (cdr l))
  5.       )
  6.   )
  7. )
Speaking English as a French Frog

Lee Mac

  • Seagull
  • Posts: 12696
  • London, England
Re: [challenge] 39 : Testing for proper lists
« Reply #5 on: August 01, 2022, 03:44:49 PM »
Another -
Code - Auto/Visual Lisp: [Select]
  1. (defun list-p ( l )
  2.     (and (< 0 (vl-list-length l))
  3.          (vl-every '(lambda ( x ) (or (atom x) (list-p x))) l)
  4.     )
  5. )

gile

  • Water Moccasin
  • Posts: 2392
  • Marseille, France
Re: [challenge] 39 : Testing for proper lists
« Reply #6 on: August 01, 2022, 05:11:45 PM »
Another -
Code - Auto/Visual Lisp: [Select]
  1. (defun list-p ( l )
  2.     (and (< 0 (vl-list-length l))
  3.          (vl-every '(lambda ( x ) (or (atom x) (list-p x))) l)
  4.     )
  5. )

I forgot vl-list-length function.
May be I misundersood, my attempts do not recursively check nested lists (i.e. consider a list containing dotted pairs as a properlist).
They simply could be written:
Code - Auto/Visual Lisp: [Select]
  1. (defun properlistp (l) (numberp (vl-list-length l)))
Speaking English as a French Frog

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
Re: [challenge] 39 : Testing for proper lists
« Reply #7 on: August 01, 2022, 06:41:54 PM »
Maybe I went too simple on my routine (compared to the other entries).

Code - Auto/Visual Lisp: [Select]
  1. (defun properp (l / my-last)
  2.    (defun my-last (lst)
  3.      (if (atom lst)
  4.        lst
  5.        (my-last (cdr lst))) )
  6.    (if l (null (my-last l))) )
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

bruno_vdh

  • Newt
  • Posts: 82
Re: [challenge] 39 : Testing for proper lists
« Reply #8 on: August 02, 2022, 03:01:46 AM »
I forgot vl-list-length function.
May be I misundersood, my attempts do not recursively check nested lists (i.e. consider a list containing dotted pairs as a properlist).
They simply could be written:
Code - Auto/Visual Lisp: [Select]
  1. (defun properlistp (l) (numberp (vl-list-length l)))
Hello,
Well seen, it will be difficult to do better
Perhaps, we can vary like this:
Code - Auto/Visual Lisp: [Select]
  1. (defun properlistp (l) (< 0 (vl-list-length l)))

 To meet this condition:
(properp nil)
> nil
Although I do not understand this request, for me nil <=> ()
Code: [Select]
_$ '()
nil
Best regards
« Last Edit: August 02, 2022, 05:49:03 AM by bruno_vdh »

Lee Mac

  • Seagull
  • Posts: 12696
  • London, England
Re: [challenge] 39 : Testing for proper lists
« Reply #9 on: August 02, 2022, 06:22:06 AM »
Another -
Code - Auto/Visual Lisp: [Select]
  1. (defun list-p ( l )
  2.     (and (< 0 (vl-list-length l))
  3.          (vl-every '(lambda ( x ) (or (atom x) (list-p x))) l)
  4.     )
  5. )

I forgot vl-list-length function.
May be I misundersood, my attempts do not recursively check nested lists (i.e. consider a list containing dotted pairs as a properlist).
They simply could be written:
Code - Auto/Visual Lisp: [Select]
  1. (defun properlistp (l) (numberp (vl-list-length l)))
Well seen, it will be difficult to do better
Perhaps, we can vary like this:
Code - Auto/Visual Lisp: [Select]
  1. (defun properlistp (l) (< 0 (vl-list-length l)))

 To meet this condition:
(properp nil)
> nil
Although I do not understand this request, for me nil <=> ()
Code: [Select]
_$ '()
nil

I too considered these, but wasn't sure whether this should be permitted:
Code - Auto/Visual Lisp: [Select]
  1. _$ (defun properlistp (l) (numberp (vl-list-length l)))
  2. PROPERLISTP
  3. _$ (properlistp '(1 2 (3 . 4)))
  4. T

gile

  • Water Moccasin
  • Posts: 2392
  • Marseille, France
Re: [challenge] 39 : Testing for proper lists
« Reply #10 on: August 02, 2022, 07:38:24 AM »
I too considered these, but wasn't sure whether this should be permitted:
Code - Auto/Visual Lisp: [Select]
  1. _$ (defun properlistp (l) (numberp (vl-list-length l)))
  2. PROPERLISTP
  3. _$ (properlistp '(1 2 (3 . 4)))
  4. T

JohnK said: "I have been told the a simple definition for a "proper list", in the Lisp languages, is the list is considered `proper` if the list's last CDR returns nil"
((0 . "CIRCLE") (10 0 0 0) (40 . 10)) is an association list and also a proper list. Last cdr returns: ((40 . 10)).
Instead of "association list" he should have said "dotted list" of which "dotted pair" is only a particular case (see here).
Speaking English as a French Frog

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
Re: [challenge] 39 : Testing for proper lists
« Reply #11 on: August 02, 2022, 12:31:20 PM »
Nice link.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org