Skip to content

Instantly share code, notes, and snippets.

@oropon
Created January 14, 2014 13:13
Show Gist options
  • Save oropon/8418103 to your computer and use it in GitHub Desktop.
Save oropon/8418103 to your computer and use it in GitHub Desktop.
module Main where
main = print $ calc cs
cs = "3 4 + 5 2 - * 4 2 / +"
calc :: String -> Float
calc = calc' [] . words
calc' :: [Float] -> [String] -> Float
calc' [x] [] = x
calc' (x':x:xs) (y:ys)
| y == "+" = calc' (x + x' : xs) ys
| y == "-" = calc' (x - x' : xs) ys
| y == "*" = calc' (x * x' : xs) ys
| y == "/" = calc' (x / x' : xs) ys
calc' stk (y:ys) = calc' (read y : stk) ys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment