Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import qualified Data.Map as Map
- import Data.List
- import Data.Char
- main = do
- input <- getLine
- let filtered = inputFilter input --just throwing out bad input
- Just operation = parser filtered --removes the just
- putStrLn (3 `operation` 7)
- inputFilter :: String -> String
- inputFilter withjunk = filter (\a -> if '*' == a || '+' == a || '-' == a then True else False) withjunk --throws out everything that's not +-*
- parser :: a -> Maybe (Int -> Int -> Int)
- parser noJunk = lookup operationMap (head noJunk) --takes the first character in there and tries to get a function out of it
- operationMap :: Map.Map (Char, (Int -> Int -> Int)) --map between operation characters and the operation functions themselves
- operationMap = Map.fromList
- [('+',Right (+))
- ,('-',Right (-))
- ,('*',Right (*))]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement