Author Topic: List Functions  (Read 7508 times)

0 Members and 1 Guest are viewing this topic.

snownut2

  • Swamp Rat
  • Posts: 971
  • Bricscad 22 Ultimate
List Functions
« on: December 28, 2013, 08:44:09 AM »
Time for some additional basic List Functions;
       ListSum = Sum of all numbers in a numbered list
       ListMax = Largest number contained in a numbered list
       ListMin  = Smallest "                                                      "

I'm sure there are tons more !!

ribarm

  • Gator
  • Posts: 3274
  • Marko Ribar, architect
Re: List Functions
« Reply #1 on: December 28, 2013, 10:06:30 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun ListSum ( lst / x xx )
  2.   (setq xx 0)
  3.   (while lst
  4.     (setq x (car lst))
  5.     (setq xx (+ x xx))
  6.     (setq lst (cdr lst))
  7.   )
  8.   xx
  9. )
  10.  
  11. (defun ListMax ( lst / x xx )
  12.   (while lst
  13.     (setq x (car lst))
  14.     (setq xx (cond (xx) ((cadr lst))))
  15.     (setq xx (max x xx))
  16.     (setq lst (cdr lst))
  17.   )
  18.   xx
  19. )
  20.  
  21. (defun ListMin ( lst / x xx )
  22.   (while lst
  23.     (setq x (car lst))
  24.     (setq xx (cond (xx) ((cadr lst))))
  25.     (setq xx (min x xx))
  26.     (setq lst (cdr lst))
  27.   )
  28.   xx
  29. )
  30.  
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: List Functions
« Reply #2 on: December 28, 2013, 10:11:55 AM »
Code: [Select]
(defun ListSum ( lst)
 (apply '+ lst)
)
 
(defun ListMax ( lst / x xx )
 (apply 'max lst)
)
 
(defun ListMin ( lst / x xx )
 (apply 'min lst)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

snownut2

  • Swamp Rat
  • Posts: 971
  • Bricscad 22 Ultimate
Re: List Functions
« Reply #3 on: December 28, 2013, 10:15:21 AM »
Code: [Select]
(defun ListSum ( lst)
 (apply '+ lst)
)
 
(defun ListMax ( lst / x xx )
 (apply 'max lst)
)
 
(defun ListMin ( lst / x xx )
 (apply 'min lst)
)

Thanks CAB, I'm not that familiar with the apply function.  This is a good example though.

Bruce

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: List Functions
« Reply #4 on: December 28, 2013, 10:17:20 AM »
CAB , you have localized variables that are not needed .

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: List Functions
« Reply #5 on: December 28, 2013, 10:22:20 AM »
I'm not that familiar with the apply function.

If you have an aversion to the apply function:
Code - Auto/Visual Lisp: [Select]
  1. (defun listsum ( l ) (eval (cons '+ l)))
  2. (defun listmin ( l ) (eval (cons 'min l)))
  3. (defun listmax ( l ) (eval (cons 'max l)))

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: List Functions
« Reply #6 on: December 28, 2013, 10:35:51 AM »
Or, if you wanted to iterate over the lists...
Code - Auto/Visual Lisp: [Select]
  1. (defun listsum ( l / r )
  2.     (setq r 0)
  3.     (foreach x l (setq r (+ x r)))
  4.     r
  5. )
  6. (defun listmin ( l / r )
  7.     (setq r (car l))
  8.     (foreach x (cdr l) (setq r (min r x)))
  9.     r
  10. )
  11. (defun listmax ( l / r )
  12.     (setq r (car l))
  13.     (foreach x (cdr l) (setq r (max r x)))
  14.     r
  15. )

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: List Functions
« Reply #7 on: December 28, 2013, 12:32:57 PM »
CAB , you have localized variables that are not needed .
Yes, I was being lazy, copy & paste from previous post.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

snownut2

  • Swamp Rat
  • Posts: 971
  • Bricscad 22 Ultimate
Re: List Functions
« Reply #8 on: December 28, 2013, 06:54:22 PM »
Lee, for the "SUM" result your last post is exactly how I had accomplished it, for the MIN/MAX I had sorted the list </> then selected the 1st element.  I was not real keen on the sort idea.

Thanks guys for the quick response's

Bruce

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: List Functions
« Reply #9 on: December 28, 2013, 07:07:16 PM »
I was not real keen on the sort idea.

Yes - sorting all items in the list is very inefficient if you are only interested in the min/max  ;-)

David Bethel

  • Swamp Rat
  • Posts: 656
Re: List Functions
« Reply #10 on: December 29, 2013, 08:51:32 AM »
Maybe flush out the functions :

Code: [Select]
;;;Product of a list
  (defun ListProd (l)
    (apply '* l))

;;;Mean or Average of a list
  (defun ListAvg (l)
    (/ (apply '+ l) (float (length l))))

;;;Median of a SORTED list
  (defun ListMed (l / c)
    (setq c (length l))
    (if (= (rem c 2) 1)
        (nth (fix (/ c 2.0)) l)
        (/ (+ (nth (1- (fix (/ c 2.0))) l)
              (nth (fix (/ c 2.0)) l))
           2.0)))

;;;Range of a list
  (defun ListRng (l / c)
    (- (apply 'max l)
       (apply 'min l)))

;;;Mode of a list
  (defun ListMode (l / c r x m)
    (setq c nil)
    (foreach a l
      (setq c (if (assoc a c)
                  (subst (list a (1+ (cadr (assoc a c))))
                         (assoc a c) c)
                  (cons (list a 1) c))))
    (setq r (mapcar ' reverse c)
          x (apply 'max (mapcar 'car r)))
    (cond ((= x 1) (setq m "None"))
          (T
           (foreach a r
             (if (= (car a) x)
                 (setq m (cons (cadr a) m))))))
    m)


Prime Factors & Least Common Multiple would be cool

-David
R12 Dos - A2K

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: List Functions
« Reply #11 on: December 29, 2013, 10:25:16 AM »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: List Functions
« Reply #12 on: December 29, 2013, 10:46:37 AM »
Least Common Multiple would be cool

A possible candidate for LCM:
Code - Auto/Visual Lisp: [Select]
  1. (defun lcm ( l / f )
  2.     (defun f ( a b / m )
  3.         (if (apply '= a)
  4.             (car a)
  5.             (progn
  6.                 (setq m (apply 'min a))
  7.                 (f (mapcar '(lambda ( a b ) (if (= a m) (+ a b) a)) a b) b)
  8.             )
  9.         )
  10.     )
  11.     (f l l)
  12. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (lcm '(22 16 31))
  2. 5456

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: List Functions
« Reply #13 on: December 29, 2013, 11:03:58 AM »
Another:
Code - Auto/Visual Lisp: [Select]
  1. (defun lcm ( l )
  2.     (/ (apply '* l) (apply '* (mapcar 'gcd l (cdr l))))
  3. )

David Bethel

  • Swamp Rat
  • Posts: 656
Re: List Functions
« Reply #14 on: December 29, 2013, 11:11:26 AM »
Another:
Code - Auto/Visual Lisp: [Select]
  1. (defun lcm ( l )
  2.     (/ (apply '* l) (apply '* (mapcar 'gcd l (cdr l))))
  3. )

That'a a good 1 !


PS

On second look, I don't think this 1 works
Code: [Select]
  (setq lst '(2 5 5 5 6 7 10 10 10 12))
  (lcm lst)
  25200
« Last Edit: December 29, 2013, 11:23:36 AM by David Bethel »
R12 Dos - A2K