parse :: Bool -> Bool -> String -> Double parse b1 b2 xs = case span (`notElem` "+-") xs of (y, x:ys ) -> parse b1 b2 y + parse (x == '+') True ys (y, _ ) -> case span (`notElem` "*/") y of (y, x:xs ) -> parse b1 b2 y * parse True (x == '*') xs ([],_ ) -> 0 (x, _ ) | b1 && b2 -> read x | b2 -> (-1) * read x | b1 && not b2 -> 1 / read x | otherwise -> -1 / read x main = interact (show . (parse True True))