In today’s Programming Praxis exercise, our goal is to implement a unix checksum utility. Let’s get started, shall we?
import Data.Char import System.Environment
I made two changes in the checksum algorithm compared to the Scheme version. I included to conversion to a string to remove some duplication and I used a simpler method of dividing and rounding up.
checksum :: String -> String checksum = (\(s,b) -> show s ++ " " ++ show (div (b + 511) 512)) . foldl (\(s,b) c -> (mod (s + ord c) 65535, b + 1)) (0,0)
Depending on whether or not the program was called with any arguments, the checksum is calculated for either the stdin input or the files provided.
main :: IO () main = getArgs >>= \args -> case args of  -> interact checksum fs -> mapM_ (\f -> putStrLn . (++ ' ':f) . checksum =<< readFile f) fs