Create a gist now

Instantly share code, notes, and snippets.

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