Averages/Root mean square: Difference between revisions

Content added Content deleted
m (→‎{{header|Haskell}}: Reduced the function passed to foldr a little)
(→‎{{header|Haskell}}: Used Data.List.genericLength in lieu of (fromIntegral . length))
Line 718: Line 718:
Or, writing a naive '''mean''' of our own, (but see https://donsbot.wordpress.com/2008/06/04/haskell-as-fast-as-c-working-at-a-high-altitude-for-low-level-performance/):
Or, writing a naive '''mean''' of our own, (but see https://donsbot.wordpress.com/2008/06/04/haskell-as-fast-as-c-working-at-a-high-altitude-for-low-level-performance/):


<lang haskell>rootMeanSquare :: [Double] -> Double
<lang haskell>import Data.List (genericLength)

rootMeanSquare xs = sqrt $ foldr ((+) . (^ 2)) 0 xs / fromIntegral (length xs)
rootMeanSquare :: [Double] -> Double
rootMeanSquare = sqrt . (((/) . foldr ((+) . (^ 2)) 0) <*> genericLength)


main :: IO ()
main :: IO ()
main = print $ rootMeanSquare [1 .. 10]</lang>
main = print $ rootMeanSquare [1 .. 10]</lang>

{{Out}}
{{Out}}
<pre>6.2048368229954285</pre>
<pre>6.2048368229954285</pre>