Created
July 6, 2015 00:40
-
-
Save tkrs/e93d036bbfabcc28a05d 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
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