-- -*- haskell -*- -- smallest -- the smallest running program doing nothing main = putStr "" -- hello world -- print a simple string on stdout main = putStrLn "Hello World" -- argv -- access command line parameters (no segmentation fault accepted, nor silent exception, so some languages must explicitly check the presence of the argument) import System main = do l <- getArgs putStrLn (head l) -- env -- access environment variable import System main = getEnv "HOME" >>= putStrLn -- test file exists -- return exit code error (non zero) if a file does not exist import Directory import System main = catch (getPermissions "/etc/mtab") (\_ -> exitFailure) -- test file readable -- return exit code error (non zero) if a file is not readable import System main = catch (readFile "/etc/mtab") (\_ -> exitFailure) -- formatting -- print integers in a simple formatted string main = putStrLn $ show a ++ " + " ++ show b ++ " = " ++ show(a + b) where a=1 b=2 -- system -- call an external program and check the return value import System import IO import Monad main = do ret <- system "false" when (ret /= ExitSuccess) (hPutStrLn stderr "false failed") system "echo done" -- sed in place -- remove #-comments from a file (modifying the file, i.e. in place) import System import System.IO import Monad import Control.Exception c = unlines . map (takeWhile (/= '#')) . lines fop f n = do l <- fmap f (readFile n) evaluate (length l) writeFile n l main = getArgs >>= (fop c) . head -- compile what must be -- find and compile .c files into .o when the .o is old or absent import System import System.IO import System.Directory import Data.List import Control.Monad gmt = getModificationTime op f = do catch (do ct <- gmt f; ot <- gmt o; when (ct > ot) comp) (\_ -> comp) where o = take (length f - 2) f ++ ".o" comp = do putStrLn ("Compiling "++f++" to "++o) system ("gcc -c -o "++o++" "++f) main = do files <- getCurrentDirectory >>= getDirectoryContents mapM_ op $ filter (".c"`isSuffixOf`) files