Cumulative standard deviation: Difference between revisions

→‎{{header|Haskell}}: Translation of AppleScript :-) (Accumulating across a fold)
(→‎{{header|Haskell}}: Translation of AppleScript :-) (Accumulating across a fold))
Line 1,666:
main = mapM_ print $ runST $
mkSD >>= forM [2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0]</lang>
 
 
Or, simply accumulating across a fold:
{{Trans|AppleScript}}
 
<lang Haskell>-- stdDevInc :: Accumulator -> (Value, Index) -> Updated Accumulator
stdDevInc :: (Float, Float, [Float]) -> (Float, Int) -> (Float, Float, [Float])
stdDevInc (sum, sqrSum, xs) (x, i) = (_sum, _sqrSum, _xs) where
_sum = sum + x
_sqrSum = sqrSum + (x^2)
_i = fromIntegral i
_xs = xs ++ [sqrt ((_sqrSum / _i) - ((_sum / _i)^2))]
sample :: [Float]
sample = [2, 4, 4, 4, 5, 5, 7, 9]
 
main = xs where
(_, _, xs) = foldl stdDevInc (0, 0, []) $ zip sample [1..length sample]</lang>
 
{{Out}}
<lang Haskell>[0.0,1.0,0.9428093,0.8660254,0.97979593,1.0,1.3997087,2.0]</lang>
 
=={{header|Haxe}}==
9,655

edits