Created
February 27, 2015 13:01
-
-
Save MiyamonY/0006cdb84a00cfa7f544 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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