Skip to content

Instantly share code, notes, and snippets.

@mdunsmuir
Created December 9, 2013 19:15
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 mdunsmuir/7879047 to your computer and use it in GitHub Desktop.
Save mdunsmuir/7879047 to your computer and use it in GitHub Desktop.
Haskell I/O sample program (Euler 18)
import System.Environment(getArgs)
main = do args <- getArgs
if length args > 0
then do inpstr <- readFile $ head args
putStrLn $ show (maxPathSum (parseData inpstr))
else putStrLn "I need a filename with triangle data!"
parseData s = map (\s -> map read (words s)) $ lines s
maxPathSum list =
let addListsFold upper lower = zipWith (+) upper $ maxList lower
where
maxList (x:[]) = []
maxList (x:y:xs) = (max x y) : (maxList $ y:xs)
in head $ foldr1 addListsFold list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment