DEV Community

Discussion on: Daily Challenge #216 - Rainfall

Collapse
 
cipharius profile image
Valts Liepiņš

Solution in Haskell:

import Data.List (find)

calcMean :: [Double] -> Double
calcMean [] = -1
calcMean xs = (foldr (+) 0 xs) / (fromIntegral . length $ xs)

calcVar :: [Double] -> Double
calcVar [] = -1
calcVar xs = calcMean . fmap diff $ xs
  where diff x = (x - calcMean xs)^2

getValues :: String -> String -> [Double]
getValues town = maybe [] parse . find theTown . lines
  where
    theTown = (==town) . takeWhile (/=':')
    parse = fmap (read . takeWhile (/=',')) . tail . words

mean :: String -> String -> Double
mean town = calcMean . getValues town

variance :: String -> String -> Double
variance town = calcVar . getValues town