Author Topic: [challenge] A03 : Find the number of elements of a list without using LENGTH  (Read 1875 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
Using only AutoLisp find the number of elements of a list without using LENGTH.
NOTE: The first element in the list is number 0.
NOTE: A NULL list should return NIL.

Example:
(my-count-list '(1 2 3 4 5 6 7))
> 6


EDIT: Added second note about returning NIL for NULL lists.
« Last Edit: December 22, 2021, 05:21:54 PM by John Kaul (Se7en) »
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12696
  • London, England
Code - Auto/Visual Lisp: [Select]
  1. (defun f1 ( l / n )
  2.     (setq n -1)
  3.     (while (setq n (1+ n) l (cdr l)))
  4.     n
  5. )

Lee Mac

  • Seagull
  • Posts: 12696
  • London, England
Assuming no duplicates:
Code - Auto/Visual Lisp: [Select]
  1. (defun f2 ( l )
  2.     (vl-position (last l) l)
  3. )

Lee Mac

  • Seagull
  • Posts: 12696
  • London, England
One more -
Code - Auto/Visual Lisp: [Select]
  1. (defun f3 ( l / n )
  2.     (setq n 0)
  3.     (while (cddddr l) (setq n (+ n 4) l (cddddr l)))
  4.     (while (setq l (cdr l)) (setq n (1+ n)))
  5.     n
  6. )

Stefan

  • Bull Frog
  • Posts: 303
Code - Auto/Visual Lisp: [Select]
  1. (defun my-count-list-stefOne (l)
  2.   (apply '+ (mapcar '(lambda (x) 1) (cdr l)))
  3. )

Edit: why length -1 ?
« Last Edit: December 17, 2021, 11:06:41 AM by Stefan »

Tharwat

  • Swamp Rat
  • Posts: 667
  • Hypersensitive
Code - Auto/Visual Lisp: [Select]
  1. (defun Length_ (lst / rtn)
  2.   ((lambda (i)
  3.      (while (and (nth (setq i (1+ i)) lst) (setq rtn i)))
  4.    )
  5.     -1
  6.   )
  7.   rtn
  8. )
  9.  


Stefan

  • Bull Frog
  • Posts: 303
Code - Auto/Visual Lisp: [Select]
  1. (defun my-count-list-stef2 (l / i)
  2.   (setq i -1)
  3.   (foreach x l
  4.     (setq i (1+ i))
  5.   )
  6. )
« Last Edit: December 17, 2021, 11:06:59 AM by Stefan »

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
Assuming no duplicates:
Code - Auto/Visual Lisp: [Select]
  1. (defun f2 ( l )
  2.     (vl-position (last l) l)
  3. )
Sorry, this is disqualified; uses VisualLisp.

Everyone else uses either the name I give or another descriptive name. Please remember these challenges are an opportunity for everyone to learn as well. While I can use my editor to make global changes (to better follow the code posted like this, instead of trying to pick out one letter variables and names) others--who use the old VLIDE, notepad, or other editors--do not have that ability. Functions that use FOO, F, BAR, L, l, etc. is obfuscated code and a bad habit. Please do not do this.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
Similar to one already posted but I had this typed up already (I am working on getting future challenges created and am currently working on challenge A18).

Code - Auto/Visual Lisp: [Select]
  1. (defun my-count-list-se7en (aList / cntr)
  2.   (if (null aList)
  3.     '()
  4.   (setq cntr -1))
  5.   (mapcar
  6.     '(lambda (x)
  7.        (set 'cntr (1+ cntr)))
  8.     aList)
  9.   cntr
  10.   )
(my-count-list-se7en '(1 2 3 4 5 6 7))
(my-count-list-se7en nil)


EDIT: Revised (added IF statement) to account for NULL lists.
« Last Edit: December 17, 2021, 10:13:28 AM by John Kaul (Se7en) »
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

gile

  • Water Moccasin
  • Posts: 2392
  • Marseille, France
Code - Auto/Visual Lisp: [Select]
  1. (defun len (l)
  2.   (if l
  3.     (1+ (len (cdr l)))
  4.     0
  5.   )
  6. )
Speaking English as a French Frog

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
Code - Auto/Visual Lisp: [Select]
  1. (defun lst_length (l)
  2.   (apply '+ (mapcar '(lambda (x) 1) (cdr l)))
  3. )

Edit: why length -1 ?

I like that!

Code - Auto/Visual Lisp: [Select]
  1. (defun len (l)
  2.   (if l
  3.     (1+ (len (cdr l)))
  4.     0
  5.   )
  6. )

That one too!
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

Stefan

  • Bull Frog
  • Posts: 303
John, please clarify what is expected on an empty list.
I guess, if '(1 2 3 4 5 6 7) -> 6, then '(1) -> 0 and '() - > nil

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
I'd say nil as well. Good call.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

gile

  • Water Moccasin
  • Posts: 2392
  • Marseille, France
John, please clarify what is expected on an empty list.
I guess, if '(1 2 3 4 5 6 7) -> 6, then '(1) -> 0 and '() - > nil
The 'native' length function does not work this way.
(length  '(1 2 3 4 5 6 7)) -> 7
(length  '(1)) - > 1
(length  '()) -> 0
Speaking English as a French Frog

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
Acknowledged but if you are using/building this function in a larger procedure you may want to control flow via an conditional like IF or COND ?easier?.
(if (my-count-list aList) ...
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org