0 Members and 2 Guests are viewing this topic.
(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 .
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](defun lcm ( l ) (/ (apply '* l) (apply '* (mapcar 'gcd l (cdr l)))))
(setq lst '(2 5 5 5 6 7 10 10 10 12)) (lcm lst) 25200