Skip to content

Instantly share code, notes, and snippets.

@alcarney
Created November 19, 2015 16:26
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 alcarney/d3c2a827a8148d7e5fa5 to your computer and use it in GitHub Desktop.
Save alcarney/d3c2a827a8148d7e5fa5 to your computer and use it in GitHub Desktop.
A simple Haskell program to count the number of lines in a given file.
import System.Environment (getArgs)
countLines :: String -> IO Int
countLines file = do
contents <- readFile file
return (length $ lines contents)
main :: IO ()
main = do
args <- getArgs
case (length args) of
0 -> putStrLn "You need to tell me which file you want to count the lines of"
_ -> let file = head args in do
putStr $ "The number of lines in the file" ++ file ++ "is: "
lines <- countLines file
putStrLn $ show lines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment