(defun ListSum ( lst)
(apply '+ lst)
)
(defun ListMax ( lst / x xx )
(apply 'max lst)
)
(defun ListMin ( lst / x xx )
(apply 'min lst)
)
Code: [Select](defun ListSum ( lst)
(apply '+ lst)
)
(defun ListMax ( lst / x xx )
(apply 'max lst)
)
(defun ListMin ( lst / x xx )
(apply 'min lst)
)
I'm not that familiar with the apply function.
CAB , you have localized variables that are not needed .Yes, I was being lazy, copy & paste from previous post.
I was not real keen on the sort idea.
;;;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)
Least Common Multiple would be cool
Another:Code - Auto/Visual Lisp: [Select]
(setq lst '(2 5 5 5 6 7 10 10 10 12))
(lcm lst)
25200
That'a a good 1 !
On second look, I don't think this 1 worksCode: [Select](setq lst '(2 5 5 5 6 7 10 10 10 12))
(lcm lst)
25200
Prime Factors would be cool
(defun pf ( n / m p r ) ; LeeMac
(setq p 2)
(while (< 1 n)
(while (zerop (rem n p))
(setq r (cons p r)
n (/ n p)))
(if (< 1 (setq m (sqrt n))
(setq p (if (= p 2) 3 (+ 2 p))))
(setq r (cons n r)
n 0)
(while (and (<= p m)
(< 0 (rem n p)))
(setq p (+ 2 p)))))
(reverse r))
(defun ListLCM (l / pl fl)
(foreach a l
(setq pl (pf a))
(foreach f pl
(if (not (member f fl))
(setq fl (cons f fl)))))
(apply '* fl))
Maybe like thus ?
Least Common Multiple - Product of the prime factors
(defun listlcm ( l / a n p r x )
(while l
(setq x (car l)
l (vl-remove x (cdr l))
p (pf x)
)
(while p
(setq x (car p)
n (length p)
p (vl-remove x (cdr p))
n (- n (length p))
a (assoc x r)
)
(if a
(if (< (cadr a) n)
(setq r (subst (list x n) a r))
)
(setq r (cons (list x n) r))
)
)
)
(apply '* (mapcar '(lambda ( x ) (apply 'expt x)) r))
)
_$ (listlcm '(2 5 5 5 6 7 10 10 10 12))
420
(listlcm '(-24 18 10)) => 90
(listlcm '( 24 -18 10)) => 120
(listlcm '( 24 18 -10)) => 72
(listlcm '(-24 -18 10)) => 10
(listlcm '(-24 18 -10)) => 18
(listlcm '( 24 -18 -10)) => 24
(listlcm '(-24 -18 -10)) => 0
The LCM of more than two integers is also well-defined: it is the smallest integer that is divisible by each of them
http://en.wikipedia.org/wiki/Least_common_multiple (http://en.wikipedia.org/wiki/Least_common_multiple)QuoteThe LCM of more than two integers is also well-defined: it is the smallest integer that is divisible by each of them
Looks like mine is it working properly......... -David
I meant to say NOT working properly. Sorry. -David