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

0 Members and 1 Guest are viewing this topic.

ronjonp

  • Needs a day job
  • Posts: 7437
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
Late to the party but I'd say it should return 0 rather than nil if you're mimicking the length function.
Code - Auto/Visual Lisp: [Select]
  1. (length nil)
  2. 0
« Last Edit: December 17, 2021, 12:13:18 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2022

Custom Build PC

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
The 'native' length function does not work this way.
(length  '(1 2 3 4 5 6 7)) -> 7
(length  '(1)) - > 1
(length  '()) -> 0

I agree. See Edit note in my first post. John decided to have it that way.

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) ...
Well, I play for the fun, but I don't see where this function might be used
(if (my-count-list aList) ... is similar to (if aList.


JohnK

  • Administrator
  • Seagull
  • Posts: 10140
I agree Stefan but this will come up again in later challenges--some of which are actually more useful functions-. I need to determine how many and we can just make this one return nil and let the later challenges error out or return what the programmer wants.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

Stefan

  • Bull Frog
  • Posts: 303
Don't worry John, it is really fun and I appreciate your initiative.

Here is another

Code - Auto/Visual Lisp: [Select]
  1. (defun my-count-list-stef3 (l)
  2.   (cond
  3.     ((cdr l) (1+ (my-count-list-stef3 (cdr l))))
  4.     (l 0)
  5.   )
  6. )
« Last Edit: December 17, 2021, 11:08:21 AM by Stefan »

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
No problem. I have a file with about 18/19 challenges typed up so far and I keep ACCORECONSOLE running on the side so I can test out any solution I type up. Works good but it's a lot of work. :)
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

apricot125

  • Mosquito
  • Posts: 13
Code: [Select]
(defun _length (lst)
  (cond ((not (cdr lst)) 0)
        (t (1+ (_length (cdr lst))))
  )
)

dexus

  • Newt
  • Posts: 75
Here is a different solution:
Code - Auto/Visual Lisp: [Select]
  1. (defun length-dexus (lst / ret)
  2.   (if (vl-consp lst)
  3.     (apply '+ (cdr (mapcar '(lambda (x) 1) lst)))
  4.   )
  5. )


Unless if vl-consp is not allowed, it would be something like this:
Code - Auto/Visual Lisp: [Select]
  1. (defun length-dexus (lst / ret)
  2.   (setq ret (apply '+ (mapcar '(lambda (x) 1) lst)))
  3.   (if (/= ret 0) (1- ret))
  4. )
« Last Edit: December 20, 2021, 07:20:01 AM by dexus »

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
Code: [Select]
(defun _length (lst)
  (cond ((not (cdr lst)) 0)
        (t (1+ (_length (cdr lst))))
  )
)
I like that, apricot125.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
Here is a different solution:
Code - Auto/Visual Lisp: [Select]
  1. (defun length-dexus (lst / ret)
  2.   (if (vl-consp lst)
  3.     (apply '+ (cdr (mapcar '(lambda (x) 1) lst)))
  4.   )
  5. )


Unless if vl-consp is not allowed, it would be something like this:
Code - Auto/Visual Lisp: [Select]
  1. (defun length-dexus (lst / ret)
  2.   (setq ret (apply '+ (mapcar '(lambda (x) 1) lst)))
  3.   (if (/= ret 0) (1- ret))
  4. )

Correct. VL-CONSP--or anything that starts with VL--is not allowed and would be disqualified.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

dexus

  • Newt
  • Posts: 75
[...]

Correct. VL-CONSP--or anything that starts with VL--is not allowed and would be disqualified.
I see, well, all vl-consp does is check if the type is a list, so this should do exactly the same thing:
Code - Auto/Visual Lisp: [Select]
  1. (defun length-dexus (lst / ret)
  2.   (if (eq (type lst) 'LIST)
  3.     (apply '+ (cdr (mapcar '(lambda (x) 1) lst)))
  4.   )
  5. )

Stefan

  • Bull Frog
  • Posts: 303
Here is a different solution:
Code - Auto/Visual Lisp: [Select]
  1. (defun length-dexus (lst / ret)
  2.   (if (vl-consp lst)
  3.     (apply '+ (cdr (mapcar '(lambda (x) 1) lst)))
  4.   )
  5. )

Not quite different...

dexus

  • Newt
  • Posts: 75
Here is a different solution:
Code - Auto/Visual Lisp: [Select]
  1. (defun length-dexus (lst / ret)
  2.   (if (vl-consp lst)
  3.     (apply '+ (cdr (mapcar '(lambda (x) 1) lst)))
  4.   )
  5. )

Not quite different...
Whoops, I didn't see that one, sorry about that.
« Last Edit: December 21, 2021, 05:29:36 AM by dexus »

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1314
  • Marco
>>> EDIT for null list (IMHO I prefer results LENGHT like…)

Code: [Select]
(defun ALE_Length (l / p c)
  (and
    l
    (setq c 0)
    (while l
      (cond
        ( (setq p (cddddr (cddddr (cddddr l)))) (setq l p  c (+ c 12)) )
        ( (setq p (cddddr (cddddr (cdddr  l)))) (setq l p  c (+ c 11)) )
        ( (setq p (cddddr (cddddr (cddr   l)))) (setq l p  c (+ c 10)) )
        ( (setq p (cddddr (cddddr (cdr    l)))) (setq l p  c (+ c  9)) )
        ( (setq p (cddddr (cddddr         l)))  (setq l p  c (+ c  8)) )
        ( (setq p (cddddr (cdddr          l)))  (setq l p  c (+ c  7)) )
        ( (setq p (cddddr (cddr           l)))  (setq l p  c (+ c  6)) )
        ( (setq p (cddddr (cdr            l)))  (setq l p  c (+ c  5)) )
        ( (setq p (cddddr                 l))   (setq l p  c (+ c  4)) )
        ( (setq p (cdddr                  l))   (setq l p  c (+ c  3)) )
        ( (setq p (cddr                   l))   (setq l p  c (+ c  2)) )
        ( (setq p (cdr                    l))   (setq l p  c (+ c  1)) )
        ( (setq l nil) )
      )
    )
  )
  c
)

Code: [Select]
Old version:
(defun ALE_LengthO (l / p c)
  (setq c 0)
  (while l
    (cond
      ( (setq p (cddddr (cddddr (cddddr l)))) (setq l p  c (+ c 12)) )
      ( (setq p (cddddr (cddddr (cdddr  l)))) (setq l p  c (+ c 11)) )
      ( (setq p (cddddr (cddddr (cddr   l)))) (setq l p  c (+ c 10)) )
      ( (setq p (cddddr (cddddr (cdr    l)))) (setq l p  c (+ c  9)) )
      ( (setq p (cddddr (cddddr         l)))  (setq l p  c (+ c  8)) )
      ( (setq p (cddddr (cdddr          l)))  (setq l p  c (+ c  7)) )
      ( (setq p (cddddr (cddr           l)))  (setq l p  c (+ c  6)) )
      ( (setq p (cddddr (cdr            l)))  (setq l p  c (+ c  5)) )
      ( (setq p (cddddr                 l))   (setq l p  c (+ c  4)) )
      ( (setq p (cdddr                  l))   (setq l p  c (+ c  3)) )
      ( (setq p (cddr                   l))   (setq l p  c (+ c  2)) )
      ( (setq p (cdr                    l))   (setq l p  c (+ c  1)) )
      ( (setq l nil) )
    )
  )
  c
)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1314
  • Marco
Code: [Select]
-- Benchmark utility: In memory of Michael Puckett ---

List length: 7
Elapsed milliseconds / relative speed for 131072 iteration(s):
    (LEE_F3 SMALLLIST)....................1500 / 1.86 <fastest>
    (LEE_F1 SMALLLIST)....................1547 / 1.81
    (GILE_LEN SMALLLIST)..................1578 / 1.77
    (MY-COUNT-LIST-STEF3 SMALLLIST).......1640 / 1.71
    (TH_LENGTH_ SMALLLIST)................1672 / 1.67
    (APR_LENGTH SMALLLIST)................1687 / 1.66
    (ALE_LENGTH SMALLLIST)................1906 / 1.47
    (MY-COUNT-LIST-SE7EN SMALLLIST).......2281 / 1.23
    (MY-COUNT-LIST-STEFONE SMALLLIST).....2734 / 1.02
    (LENGTH-DEXUS SMALLLIST)..............2797 / 1 <slowest>

List length: 82
Elapsed milliseconds / relative speed for 65536 iteration(s):
    (ALE_LENGTH MEDIUMLIST)................1063 / 2.45 <fastest>
    (LEE_F3 MEDIUMLIST)....................1094 / 2.38
    (LEE_F1 MEDIUMLIST)....................1656 / 1.58
    (GILE_LEN MEDIUMLIST)..................1765 / 1.48
    (MY-COUNT-LIST-STEFONE MEDIUMLIST).....1937 / 1.35
    (LENGTH-DEXUS MEDIUMLIST)..............1938 / 1.35
    (MY-COUNT-LIST-STEF3 MEDIUMLIST).......2172 / 1.2
    (MY-COUNT-LIST-SE7EN MEDIUMLIST).......2344 / 1.11
    (TH_LENGTH_ MEDIUMLIST)................2594 / 1.01
    (APR_LENGTH MEDIUMLIST)................2609 / 1 <slowest>

List length: 4100
Elapsed milliseconds / relative speed for 8192 iteration(s):
    (ALE_LENGTH BIGLIST)................1828 / 4 <fastest>
    (LEE_F3 BIGLIST)....................3094 / 2.36
    (LENGTH-DEXUS BIGLIST)..............3938 / 1.86
    (MY-COUNT-LIST-STEFONE BIGLIST).....3969 / 1.84
    (LEE_F1 BIGLIST)....................6688 / 1.09
    (GILE_LEN BIGLIST)..................7312 / 1 <slowest>

List length: 4198400
Elapsed milliseconds / relative speed for 8 iteration(s):
    (ALE_LENGTH ULTRALIST)................1015 / 3.71 <fastest>
    (LEE_F3 ULTRALIST)....................2344 / 1.61
    (LENGTH-DEXUS ULTRALIST)..............3687 / 1.02
    (MY-COUNT-LIST-STEFONE ULTRALIST).....3765 / 1 <slowest>

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1314
  • Marco
Code: [Select]
; "strange" version - length <= 100
(defun ALE_LengthNth (L)
  (nth 100 (append L
   '( 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80
      79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59
      58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38
      37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17
      16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
    )
  )       )
)