Skip to content

Instantly share code, notes, and snippets.

@refi64
Created May 14, 2015 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save refi64/bda3d88a28e7a7542652 to your computer and use it in GitHub Desktop.
Save refi64/bda3d88a28e7a7542652 to your computer and use it in GitHub Desktop.
Haskell version of "Four MLs (and a Python)"
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