Averages/Pythagorean means: Difference between revisions

→‎Haskell - Three applicatively defined functions: Simplifed the three functions with a liftM2 (locally defined, rather than imported)
(→‎{{header|Haskell}}: Added an alternative, defining the functions applicatively, and testing with (and zipWith (>=)))
(→‎Haskell - Three applicatively defined functions: Simplifed the three functions with a liftM2 (locally defined, rather than imported))
Line 1,242:
-- ARITHMETIC, GEOMETRIC AND HARMONIC MEANS -----------------------
arithmetic, geometric, harmonic :: [Double] -> Double
arithmetic = pureliftM2 (/) <*> sum <*> genericLength
 
geometric = pureliftM2 (**) <*> product <*> ((1 /) . genericLength)
 
harmonic = pureliftM2 (/) <*> genericLength <*> (foldr ((+) . (1 /)) 0)
 
-- GENERIC --------------------------------------------------------
liftM2 f g h = pure f <*> g <*> h
 
-- TEST -----------------------------------------------------------
9,655

edits