Created
September 9, 2012 08:17
-
-
Save aomoriringo/3683299 to your computer and use it in GitHub Desktop.
This file contains 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
import Data.Char | |
import System.IO | |
import System.Environment | |
main = do | |
(command:argList) <- getArgs | |
dispatch command argList | |
dispatch :: String -> [String] -> IO () | |
dispatch "add" = calc (+) | |
dispatch "sub" = calc (-) | |
dispatch "times" = calc (*) | |
dispatch "div" = calc (/) | |
dispatch command = doesntExist command | |
calc :: (Read a, Show b) => (a -> a -> b) -> [String] -> IO() | |
calc p [x,y] = putStrLn . show $ p (read x) (read y) | |
calc p _ = putStrLn "This command takes exactly two arguments" | |
doesntExist :: String -> [String] -> IO() | |
doesntExist command _ = | |
putStrLn $ "The " ++ command ++ " command doesn't exist" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment