Cumulative standard deviation: Difference between revisions

Content added Content deleted
(→‎{{header|Forth}}: Fixed to return the running standard deviation)
Line 2,906: Line 2,906:


=={{header|Scheme}}==
=={{header|Scheme}}==
{{works with|Racket}}
{{incorrect|Scheme|running-stddev procedure only in Racket? Usage example missing, and not evident.}}
<lang scheme>
<lang scheme>
(define ((running-stddev . nums) num)
(define (standart-deviation-generator)
(set! nums (cons num nums))
(let ((nums '()))
(lambda (x)
(sqrt (- (/ (apply + (map (lambda (i) (* i i)) nums)) (length nums)) (expt (/ (apply + nums) (length nums)) 2))))
(set! nums (cons x nums))
(let* ((mean (/ (apply + nums) (length nums)))
(mean-sqr (lambda (y) (expt (- y mean) 2)))
(variance (/ (apply + (map mean-sqr nums)) (length nums))))
(sqrt variance)))))

(let loop ((f (standart-deviation-generator))
(input '(2 4 4 4 5 5 7 9)))
(if (not (null? input))
(begin
(display (f (car input)))
(newline)
(loop f (cdr input)))))
</lang>
</lang>