fork(1) download
  1. parse :: Bool -> Bool -> String -> Double
  2. parse b1 b2 xs = case span (`notElem` "+-") xs of
  3. (y, x:ys ) -> parse b1 b2 y + parse (x == '+') True ys
  4. (y, _ ) -> case span (`notElem` "*/") y of
  5. (y, x:xs ) -> parse b1 b2 y * parse True (x == '*') xs
  6. ([],_ ) -> 0
  7. (x, _ )
  8. | b1 && b2 -> read x
  9. | b2 -> (-1) * read x
  10. | b1 && not b2 -> 1 / read x
  11. | otherwise -> -1 / read x
  12.  
  13. main = interact (show . (parse True True))
  14.  
  15.  
Success #stdin #stdout 0s 4824KB
stdin
3 + 100/10/2/5 - 4 - 4/2
stdout
-2.0