Subject: Re: Q: total addition within an array. (any function or macro?)
From: Erik Naggum <erik@naggum.net>
Date: 15 Dec 2000 17:42:47 +0000
Newsgroups: comp.lang.lisp
Message-ID: <3185890967472817@naggum.net>

* sungwoo@cad.strath.ac.uk
| One condition is, I don't know the total length of an array because it
| will be changed every time.  Maybe I could write down as (although it
| looks complicated..),
| 
| (setf total-value 0)
| (loop for i from 0 to arr-index
|          do (setf total-value (+ total-value (aref arr i)))
|          finally (setf ave-value (/ total-value (+ arr-index 1))))
| 
| However, I just wonder that is there any function or macro which can
| add whole values in an array?  I want use this function with one
| simple line.... I couldn't find that kind of things, but maybe
| something exist? Thanks for advance. =)

  I should give you the hints and the pointers, but I'm tired, so here's
  how you compute the average of a vector of numbers:

              (/ (reduce #'+ <vector>) (length <vector>))

  Incidentally, the standard deviation is also very useful if you ever
  work with averages:

(let* ((mean (/ (reduce #'+ <vector>) (length <vector>)))
       (sdev (sqrt (/ (reduce #'+ <vector> :key #'(lambda (elt) (expt (- elt mean) 2)))
		      (1- (length <vector>))))))
  ...)

  As you may have noticed, this is idiomatic Common Lisp for the sum
  operator.

#:Erik
-- 
  The United States of America, soon a Bush league world power.  Yeee-haw!