Skip to content

Instantly share code, notes, and snippets.

@MiyamonY
Created February 27, 2015 13:01
Show Gist options
  • Save MiyamonY/0006cdb84a00cfa7f544 to your computer and use it in GitHub Desktop.
Save MiyamonY/0006cdb84a00cfa7f544 to your computer and use it in GitHub Desktop.
solveRPN :: String -> Double
solveRPN = head . foldl foldingFunction [] . words
where foldingFunction (x:y:ys) "*" = (y * x) : ys
foldingFunction (x:y:ys) "+" = (y + x) : ys
foldingFunction (x:y:ys) "-" = (y - x) : ys
foldingFunction (x:y:ys) "/" = (y / x) : ys
foldingFunction (x:y:ys) "^" = (y ** x) : ys
foldingFunction (x:xs) "ln" = log x : xs
foldingFunction xs "sum" = [sum xs]
foldingFunction xs numberString = read numberString : xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment