Jump to content

Averages/Pythagorean means: Difference between revisions

m
m (→‎Three applicatively defined functions: liftA2 -> ((.) ... <*>))
Line 1,259:
 
====Three applicatively defined functions====
These three functions (each combining the length of a list with some kind of fold over the elements of that same list), all share the same general '''liftA2'''applicative structure.
 
<lang haskell>import ControlData.ApplicativeList (liftA2genericLength)
import Data.List (genericLength)
 
-- ARITHMETIC, GEOMETRIC AND HARMONIC MEANS ---------------
 
arithmetic, geometric, harmonic :: [Double] -> Double
arithmetic = liftA2 (/) . sum <*> genericLength
 
geometric = liftA2 (**) . product <*> ((1 /) . genericLength)
 
harmonic = liftA2 (/) . genericLength (<*> foldr ((+) . (1 /)) 0)
 
-- TEST ---------------------------------------------------
Line 1,283 ⟶ 1,281:
, mappend "\n A >= G >= H is " $ --
(show . and) $ zipWith (>=) xs (tail xs)
]</lang>
</lang>
{{Out}}
<pre>("Arithmetic",5.5)("Geometric",4.528728688116765)("Harmonic",3.414171521474055)
9,655

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.