Author Topic: [challenge] A15 : list length  (Read 516 times)

0 Members and 1 Guest are viewing this topic.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
[challenge] A15 : list length
« on: January 19, 2022, 07:07:10 AM »
you need to calculate the length of any list, for example:

Code - Auto/Visual Lisp: [Select]
  1. (list-length '(1 2 3))                                              ; >> 3
  2. (list-length '(1 (2 (3))))                                          ; >> 3
  3. (list-length '(1 (2 ((3 . 4) 5) 6) 7))                              ; >> 7
  4. (list-length '(nil ("a" ((1 . 1) 2) 2) nil))                        ; >> 7
  5. (list-length '(nil))                                                ; >> 1
  6. (list-length nil)                                                   ; >> 0

gile

  • Water Moccasin
  • Posts: 2392
  • Marseille, France
Re: [challenge] A15 : list length
« Reply #1 on: January 19, 2022, 09:37:58 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun list-length (l)
  2.   (cond ((null l) 0)
  3.         ((atom l) 1)
  4.         ((atom (car l)) (1+ (list-length (cdr l))))
  5.         (T (+ (list-length (car l)) (list-length (cdr l))))
  6.   )
  7. )
Speaking English as a French Frog

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
Re: [challenge] A15 : list length
« Reply #2 on: January 19, 2022, 09:55:28 AM »
Some of those conditions are just mean! :) I've made one quick attempt but I failed so I'll have to find some time in a bit to try again.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: [challenge] A15 : list length
« Reply #3 on: January 19, 2022, 10:23:27 AM »
Some of those conditions are just mean! :) I've made one quick attempt but I failed so I'll have to find some time in a bit to try again.

You prepared well while I was away. This task massage the brain a bit.

Code - Auto/Visual Lisp: [Select]
  1. (defun list-length (l)
  2.   (cond ((null l) 0)
  3.         ((atom l) 1)
  4.         ((atom (car l)) (1+ (list-length (cdr l))))
  5.         (T (+ (list-length (car l)) (list-length (cdr l))))
  6.   )
  7. )
Perfect solution! I wrote - word for word, while preparing this topic.

kirby

  • Newt
  • Posts: 110
Re: [challenge] A15 : list length
« Reply #4 on: January 19, 2022, 10:38:12 AM »
Couldn't wait for lunchtime...
(too much fun, also beats work)

Code - Auto/Visual Lisp: [Select]
  1. (defun C:TestListLength ( / MyTestList MyItem CNT Ans)
  2. ; Test routine for Challenge A15
  3.  
  4. (setq MyTestList (list
  5.         '(1 2 3)                                ; >> 3
  6.         '(1 (2 (3)))                            ; >> 3
  7.         '(1 (2 ((3 . 4) 5) 6) 7)                ; >> 7
  8.         '(nil ("a" ((1 . 1) 2) 2) nil)          ; >> 7
  9.         '(nil)                                  ; >> 1
  10.         nil                                     ; >> 0
  11. ))
  12.  
  13. (setq CNT 0)
  14. (repeat (length MyTestList)
  15.         (setq MyItem (nth CNT MyTestList))
  16.         (setq Ans (List-Length-Kirby MyItem))  
  17.        
  18.         (prompt "\nList = ")(princ MyItem)(prompt "  List Length = ")(princ Ans)
  19.         (setq CNT (1+ CNT))
  20. ) ; close repeat       
  21. )
  22.  
  23.  
  24. (defun List-Length-Kirby (MyList / Verbose OutVal Count CNT NewCount MyItem)
  25. ; Challenge A15 : List length
  26. ; Input:
  27. ;       MyList - a list
  28. ; Returns:
  29. ;       count of list items including items in sublists
  30. ;       If MyList is not a list or nil, return 0
  31.  
  32. (setq Verbose 0)        ; =1 to show debug info
  33. (setq OutVal 0)
  34.  
  35. (if (eq (type MyList) 'LIST)
  36.   (progn
  37.         (setq Count 0)
  38.  
  39.         (setq CNT 0)
  40.         (repeat (length MyList)                         ; Uses 'Length' command, does this disqualify?  Could use recursion
  41.                 (setq MyItem (nth CNT MyList))
  42.        
  43.                 (if (eq Verbose 1)
  44.                   (progn
  45.                         ; Show debug
  46.                         (prompt "\n  CNT = ")(princ CNT)(prompt "  Item = ")(princ MyItem)(prompt "  Type = ")(princ (type MyItem))(princ)
  47.                   )
  48.                 )
  49.        
  50.                 (cond
  51.                         ((dottedpairp MyItem) (setq NewCount 2))                                ; Custom function.  Thanks Giles!
  52.                         ((eq (type MyItem) 'List) (setq NewCount (List-Length-Kirby MyItem)))   ; Yikes, recursion
  53.                         ((eq MyItem nil) (setq NewCount 1))                                     ; nil counts as 1
  54.                         ('T (setq NewCount 1))                                                  ; Not a list, count is 1
  55.                 )      
  56.        
  57.        
  58.                 (setq Count (+ Count NewCount))
  59.                 (if (eq Verbose 1)
  60.                   (progn
  61.                         ; Show debug
  62.                         (prompt "\n    NewCount = ")(princ NewCount)(prompt "  Total Count = ")(princ Count)(princ)
  63.                   )
  64.                 )
  65.        
  66.                 (setq CNT (1+ CNT))
  67.         ) ; close repeat
  68.         (setq OutVal Count)
  69.   )
  70. ) ; close if
  71.  
  72. OutVal
  73. )
  74.  
  75.  
  76. (defun dottedPairp (p)
  77. ; Test for dotted pair, by Gile (Gilles Chanteau)
  78. ; http://www.theswamp.org/index.php?topic=42940.msg482148#msg482148
  79. ; Returns T is dotted pair and nil if not
  80.         (and (listp p) (cdr p) (atom (cdr p)))
  81. )
  82.  
  83.  

bruno_vdh

  • Newt
  • Posts: 82
Re: [challenge] A15 : list length
« Reply #5 on: January 19, 2022, 12:27:59 PM »
Hi,
Code - Auto/Visual Lisp: [Select]
  1. (defun list-length (l)
  2.   (cond ((null l) 0)
  3.         ((atom l) 1)
  4.         ((atom (car l)) (1+ (list-length (cdr l))))
  5.         (T (+ (list-length (car l)) (list-length (cdr l))))
  6.   )
  7. )

Congratulations, Gile is the fastest, to console me I give the iterative version...
Code - Auto/Visual Lisp: [Select]
  1. (defun list-length-vdh (l / m n)
  2.   (setq n 0)
  3.   (while (or l m)
  4.     (cond ((null l) (setq l (car m) m (cdr m)))
  5.           ((atom l) (setq n (1+ n) l (car m) m (cdr m)))
  6.           ((atom (car l)) (setq n (1+ n) l (cdr l)))
  7.           (T (setq m (cons (cdr l) m) l (car l)))
  8.     )
  9.   )
  10.   n
  11. )

« Last Edit: January 19, 2022, 12:54:26 PM by bruno_vdh »

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1314
  • Marco
Re: [challenge] A15 : list length
« Reply #6 on: January 19, 2022, 01:20:59 PM »
Code: [Select]
(defun LengthN_ALE (L / Fx c)
  (setq c 0)
  (defun Fx (L n)
    (foreach e L
      (setq n (1+ n))
      (or
        (atom e)
        (if (listp (cdr e))
          (setq n (1- (Fx e n)))
          (setq n (1+ n))
        )
      )
    )
    n
  )
  (Fx L c)
)
more readable...
Code: [Select]
(defun LengthN_ALE2 (L / Fx c)
  (setq c 0)
  (defun Fx (L n)
    (foreach e L
      (cond
        ( (atom e)        (setq n (1+   n)) )
        ( (listp (cdr e)) (setq n (Fx e n)) )
        ( T               (setq n (+ 2  n)) )
      )
    )
    n
  )
  (Fx L c)
)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: [challenge] A15 : list length
« Reply #7 on: January 19, 2022, 03:56:26 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun f (l)
  2.   (cond ((not l) 0)
  3.         ((atom l) 1)
  4.         ((+ (if (atom (car l)) 1 (f (car l))) (f (cdr l))))
  5.   )
  6. )

bruno_vdh

  • Newt
  • Posts: 82
Re: [challenge] A15 : list length
« Reply #8 on: January 19, 2022, 04:08:36 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun f (l)
  2.   (cond ((not l) 0)
  3.         ((atom l) 1)
  4.         ((+ (if (atom (car l)) 1 (f (car l))) (f (cdr l))))
  5.   )
  6. )
Congratulations, the factorization had escaped me.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1314
  • Marco
Re: [challenge] A15 : list length
« Reply #9 on: January 20, 2022, 03:10:08 AM »
Mapcar version:
Code: [Select]
(defun LengthN_ALE3 (L / Fx c)
  (setq c 0)
  (defun Fx (L n)
    (mapcar
     '(lambda (e)
        (cond
          ( (atom e)        (setq n (1+   n)) )
          ( (listp (cdr e)) (setq n (Fx e n)) )
          ( T               (setq n (+ 2  n)) )
        )
      )
      L
    )
    n
  )
  (Fx L c)
)