Created
July 20, 2015 06:29
-
-
Save shigemk2/7c04975586460ee17067 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 | |
| -- 状態は引数としては明示的に書かない | |
| pn str = fst $ pnPrime $ splitOn " " str | |
| pnPrime ("+":s0) = | |
| let (a, s1) = pnPrime s0 | |
| (b, s2) = pnPrime s1 | |
| in (a + b, s2) | |
| pnPrime (x:xs) = ((read x:: Int), xs) | |
| -- evalStateで値を取り出す | |
| pnM str = evalState pnPrimeM $ splitOn " " str | |
| pnPrimeM :: State [String] Int | |
| pnPrimeM = do | |
| -- 状態をxsに更新する | |
| (x:xs) <- get | |
| put xs | |
| case x of | |
| "+" -> do | |
| -- ただの<-で値を取り出す | |
| a <- pnPrimeM | |
| b <- pnPrimeM | |
| return $ a + b | |
| _ -> do | |
| return $ read x | |
| main = do | |
| print $ pnM "1" | |
| print $ pnM "+ 1 2" | |
| print $ pnM "+ 1 + 2 3" | |
| print $ pnM "+ + 1 2 3" | |
| print $ pnM "+ + 1 2 + 3 4" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment