Tuesday, July 24, 2007

Average of a List

A quick example of how to take the average of a list in one pass:



listAvg :: (Fractional b) => [b] -> b
listAvg = uncurry (/) . foldl' (\(sum,cnt) x -> (sum + x, cnt + 1)) (0,0)