Skip to content

Instantly share code, notes, and snippets.

@trevorc
Created October 17, 2011 02:24
Show Gist options
  • Save trevorc/1291809 to your computer and use it in GitHub Desktop.
Save trevorc/1291809 to your computer and use it in GitHub Desktop.
Haskell RPN calculator
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