Skip to content

Instantly share code, notes, and snippets.

@tkrs
Created July 6, 2015 00:40
Show Gist options
  • Save tkrs/e93d036bbfabcc28a05d to your computer and use it in GitHub Desktop.
Save tkrs/e93d036bbfabcc28a05d to your computer and use it in GitHub Desktop.
reverse-polish-notation
import System.Environment
main = do
expr <- getArgs
print $ head $ foldl rpn [] expr
rpn :: [Double] -> String -> [Double]
rpn [] a = [read a]
rpn xs@[_] a = (read a) : xs
rpn xs@(x:y:xs') a | a == "+" = (x + y) : xs'
| a == "-" = (x - y) : xs'
| a == "*" = (x * y) : xs'
| a == "/" = (x / y) : xs'
| otherwise = (read a) : xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment