Skip to content

Instantly share code, notes, and snippets.

@hamzamuric
Created September 6, 2021 14:02
Show Gist options
  • Save hamzamuric/a3816f7f27d547448d39753812e48716 to your computer and use it in GitHub Desktop.
Save hamzamuric/a3816f7f27d547448d39753812e48716 to your computer and use it in GitHub Desktop.
Reverse Polish Notation Calculator
main = do
content <- getLine
print $ calculate content
calculate :: String -> Float
calculate = head . foldl f [] . words
f :: [Float] -> String -> [Float]
f (x:y:rest) "+" = (x + y):rest
f (x:y:rest) "-" = (y - x):rest
f (x:y:rest) "*" = (x * y):rest
f (x:y:rest) "/" = (x / y):rest
f acc el = read el:acc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment