From ... Path: supernews.google.com!sn-xit-02!supernews.com!216.227.56.88.MISMATCH!telocity-west!TELOCITY!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!uio.no!Norway.EU.net!127.0.0.1!nobody From: Erik Naggum Newsgroups: comp.lang.lisp Subject: Re: Q: total addition within an array. (any function or macro?) Date: 15 Dec 2000 17:42:47 +0000 Organization: Naggum Software; vox: +47 800 35477; gsm: +47 93 256 360; fax: +47 93 270 868; http://naggum.no; http://naggum.net Lines: 34 Message-ID: <3185890967472817@naggum.net> References: <91dcr2$3n$1@nnrp1.deja.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: oslo-nntp.eunet.no 976904222 12481 195.0.192.66 (15 Dec 2000 18:17:02 GMT) X-Complaints-To: newsmaster@eunet.no NNTP-Posting-Date: 15 Dec 2000 18:17:02 GMT mail-copies-to: never User-Agent: Gnus/5.0803 (Gnus v5.8.3) Emacs/20.7 Xref: supernews.google.com comp.lang.lisp:5336 * 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 #'+ ) (length )) Incidentally, the standard deviation is also very useful if you ever work with averages: (let* ((mean (/ (reduce #'+ ) (length ))) (sdev (sqrt (/ (reduce #'+ :key #'(lambda (elt) (expt (- elt mean) 2))) (1- (length )))))) ...) 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!