[plt-scheme] Statistics for Sequences

From: Marco Monteiro (masm at acm.org)
Date: Wed Sep 9 15:32:51 EDT 2009

On Wed, Sep 9, 2009 at 4:37 PM, Doug Williams
<m.douglas.williams at gmail.com>wrote:

> Thanks for running them for me. I guess it comes down to whether the
> flexibility is worth the performance hit. I like the flexibility. In the
> past there were times I have had to convert lists to vectors just to compute
> statistics on them, which is even less efficient.  I could include the old
> ones as vector-mean, vector-variance, etc for people who need/want the
> performance.
>
> Doug
>
>
Why not special case for vectors instead of adding to the module interface?

(define (mean data)
  (if (vector? data) (vector-mean data) (generic-mean data))

(define (generic-mean data)
  (for/fold ((m-old 0.0))
            ((i (in-naturals))
             (x data))
    (+ m-old (/ (- x m-old) (add1 i)))))

(define (vector-mean data)
  (let ((n (vector-length data))
        (mu 0.0))
    (do ((i 0 (+ i 1)))
        ((= i n) mu)
      (set! mu (+ mu (/ (- (vector-ref data i) mu) (+ i 1)))))))

--
Marco
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090909/f3afee4f/attachment.html>

Posted on the users mailing list.