Averages/Arithmetic mean: Difference between revisions

Content added Content deleted
(→‎{{header|Common Lisp}}: Simpler version using apply)
(→‎{{header|Common Lisp}}: replaced; now applicable to all sequence types and uses REDUCE, not APPLY (good practice); 'sum-list' is gone as it had bad syntax)
Line 80: Line 80:
=={{header|Common Lisp}}==
=={{header|Common Lisp}}==


(defun mean (sequence)
This example uses a recursive sum-list function.
(let ((length (length sequence)))

(if (zerop length)
(defun sum-list (list)
(if (list)
0
(+ (car list) (sum-list (cdr list)))
(/ (reduce #'+ sequence) length))))
0))
(defun mean (list)
(/ (sum-list list) (length list)))

However the following works as well:

(defun mean (list)
(if list
(/ (apply '+ list) (length list))
0))


=={{header|Forth}}==
=={{header|Forth}}==