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

0 Members and 1 Guest are viewing this topic.

JohnK

• Seagull
• Posts: 10140
[challenge] A03 : Find the number of elements of a list without using LENGTH
« on: December 17, 2021, 09:14:35 AM »
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

« 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
Re: [challenge] A03 : Find the number of elements of a list without using LENGTH
« Reply #1 on: December 17, 2021, 09:18:14 AM »
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
Re: [challenge] A03 : Find the number of elements of a list without using LENGTH
« Reply #2 on: December 17, 2021, 09:19:34 AM »
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
Re: [challenge] A03 : Find the number of elements of a list without using LENGTH
« Reply #3 on: December 17, 2021, 09:23:14 AM »
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
Re: [challenge] A03 : Find the number of elements of a list without using LENGTH
« Reply #4 on: December 17, 2021, 09:29:41 AM »
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
Re: [challenge] A03 : Find the number of elements of a list without using LENGTH
« Reply #5 on: December 17, 2021, 09:30:57 AM »
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
Re: [challenge] A03 : Find the number of elements of a list without using LENGTH
« Reply #6 on: December 17, 2021, 09:40:30 AM »
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

• Seagull
• Posts: 10140
Re: [challenge] A03 : Find the number of elements of a list without using LENGTH
« Reply #7 on: December 17, 2021, 09:47:08 AM »
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

• Seagull
• Posts: 10140
Re: [challenge] A03 : Find the number of elements of a list without using LENGTH
« Reply #8 on: December 17, 2021, 09:50:04 AM »
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.     '(lambda (x)
6.        (set 'cntr (1+ cntr)))
7.     aList)
8.   cntr
9.   )
(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
Re: [challenge] A03 : Find the number of elements of a list without using LENGTH
« Reply #9 on: December 17, 2021, 09:52:14 AM »
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

• Seagull
• Posts: 10140
Re: [challenge] A03 : Find the number of elements of a list without using LENGTH
« Reply #10 on: December 17, 2021, 09:52:51 AM »
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
Re: [challenge] A03 : Find the number of elements of a list without using LENGTH
« Reply #11 on: December 17, 2021, 10:00:14 AM »
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

• Seagull
• Posts: 10140
Re: [challenge] A03 : Find the number of elements of a list without using LENGTH
« Reply #12 on: December 17, 2021, 10:02:57 AM »
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
Re: [challenge] A03 : Find the number of elements of a list without using LENGTH
« Reply #13 on: December 17, 2021, 10:18:35 AM »
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