Created
May 14, 2015 21:35
-
-
Save refi64/bda3d88a28e7a7542652 to your computer and use it in GitHub Desktop.
Haskell version of "Four MLs (and a Python)"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Applicative | |
import System.Environment | |
import Data.List.Split | |
import Data.List | |
-- the ":: [Float]" prevents the type inferencer from going haywire | |
addTo [] v = v :: [Float] | |
addTo t v | |
| length t == length v = zipWith (+) t v | |
| otherwise = error "Inconsistent-length rows" | |
addLineTo t l = addTo t . map read $ splitOn "," l | |
sumData = foldl addLineTo [] . lines | |
sumAndPrintFile f = intercalate "," <$> map show <$> sumData <$> readFile f >>= putStrLn | |
parseArgs [f] = sumAndPrintFile f | |
parseArgs _ = error "Exactly 1 filename must be given" | |
main = getArgs >>= parseArgs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment