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}}== |
||
⚫ | |||
This example uses a recursive sum-list function. |
|||
(let ((length (length sequence))) |
|||
(if (zerop length) |
|||
(defun sum-list (list) |
|||
0 |
|||
(/ (reduce #'+ sequence) length)))) |
|||
0)) |
|||
⚫ | |||
(/ (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}}== |