Created
October 17, 2011 02:24
-
-
Save trevorc/1291809 to your computer and use it in GitHub Desktop.
Haskell RPN calculator
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
module Main where | |
import System.Environment | |
runRPN :: (Fractional a, Read a) => String -> [a] | |
runRPN = foldl doToken [] . words | |
where doToken (x:y:zs) "+" = (y+x):zs | |
doToken (x:y:zs) "-" = (y-x):zs | |
doToken (x:y:zs) "*" = (y*x):zs | |
doToken (x:y:zs) "/" = (y/x):zs | |
doToken a n = read n:a | |
main :: IO () | |
main = do | |
args <- getArgs | |
print $ head $ runRPN $ unwords args |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment