Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import qualified Data.Map as Map
  2. import Data.List
  3. import Data.Char
  4.  
  5. main = do
  6.     input <- getLine
  7.     let filtered = inputFilter input --just throwing out bad input
  8.         Just operation = parser filtered --removes the just
  9.     putStrLn (3 `operation` 7)
  10.  
  11. inputFilter :: String -> String
  12. inputFilter withjunk = filter (\a -> if '*' == a || '+' == a || '-' == a then True else False) withjunk --throws out everything that's not +-*
  13.  
  14. parser :: a -> Maybe (Int -> Int -> Int)
  15. parser noJunk = lookup operationMap (head noJunk) --takes the first character in there and tries to get a function out of it
  16.  
  17. operationMap :: Map.Map (Char, (Int -> Int -> Int)) --map between operation characters and the operation functions themselves
  18. operationMap = Map.fromList
  19.     [('+',Right (+))
  20.     ,('-',Right (-))
  21.     ,('*',Right (*))]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement