Skip to content

Instantly share code, notes, and snippets.

@aomoriringo
Created September 9, 2012 08:17
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 aomoriringo/3683299 to your computer and use it in GitHub Desktop.
Save aomoriringo/3683299 to your computer and use it in GitHub Desktop.
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