Created
March 3, 2018 19:40
-
-
Save jehugaleahsa/25bdac77d8f260c9ae602ca40359e30d to your computer and use it in GitHub Desktop.
Reverse Polish Notation
This file contains 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
calculate :: [String] -> Maybe Double | |
calculate [] = Nothing | |
calculate a = calculateHelper a [] | |
where | |
calculateHelper [] [a] = Just a | |
calculateHelper ("+":items) (x:y:rest) = calculateHelper items ((y+x):rest) | |
calculateHelper ("-":items) (x:y:rest) = calculateHelper items ((y-x):rest) | |
calculateHelper ("*":items) (x:y:rest) = calculateHelper items ((y*x):rest) | |
calculateHelper ("/":items) (x:y:rest) = calculateHelper items ((y/x):rest) | |
calculateHelper (a:items) rest = case (readMaybe a :: Maybe Double) of | |
Just a -> calculateHelper items (a:rest) | |
Nothing -> Nothing | |
calculateHelper _ _ = Nothing | |
parts = words "10 4 3 + 2 * -" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment