Created
April 22, 2011 04:03
-
-
Save mcandre/935992 to your computer and use it in GitHub Desktop.
Hashmat the brave warrior
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env runhaskell | |
-- Andrew Pennebaker | |
-- 21 April 2011 | |
import Data.String.Utils (split) | |
import Data.List (intercalate) | |
-- Thanks to aavogt at #haskell | |
maybeRead :: Read a => String -> Maybe a | |
maybeRead s = case reads s of | |
(r, ""):[] -> Just r | |
_ -> Nothing | |
diff :: String -> String -> Int | |
diff sa sb = case (maybeRead sa, maybeRead sb) of | |
(Just a, Just b) -> abs (a - b) | |
_ -> 0 | |
diffLine :: String -> String | |
diffLine input = unlines $ map diffLine' $ lines input | |
where | |
diffLine' :: String -> String | |
diffLine' line = show $ case split " " line of | |
a:b:_ -> diff a b | |
_ -> 0 | |
main :: IO () | |
main = interact diffLine |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment