Created
July 20, 2015 07:22
-
-
Save shigemk2/2a2377e84b1010d0d33a 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
| import Data.List.Split | |
| import Control.Monad.State | |
| rpn str = rpnPrime (splitOn " " str) [] | |
| rpnPrime ("+":t) (y:x:zs) = rpnPrime t $ x + y:zs | |
| rpnPrime (n:t) zs = rpnPrime t $ read n:zs | |
| rpnPrime [] [x] = x | |
| rpnM str = evalState (rpnPrimeM $ splitOn " " str) [] | |
| rpnPrimeM :: [String] -> State [Int] Int | |
| rpnPrimeM ("+":t) = do | |
| (y:x:zs) <- get | |
| put $ x + y:zs | |
| rpnPrimeM t | |
| rpnPrimeM (n:t) = do | |
| zs <- get | |
| put $ read n:zs | |
| rpnPrimeM t | |
| rpnPrimeM [] = do | |
| [x] <- get | |
| return x | |
| main = do | |
| print $ rpnM "1" | |
| print $ rpnM "1 2 +" | |
| print $ rpnM "1 2 3 + +" | |
| print $ rpnM "1 2 + 3 +" | |
| print $ rpnM "1 2 + 3 4 + +" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment