結果

問題 No.222 引き算と足し算
ユーザー ducktailducktail
提出日時 2018-05-10 16:10:12
言語 Haskell
(9.8.2)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 777 bytes
コンパイル時間 601 ms
コンパイル使用メモリ 158,208 KB
最終ジャッジ日時 2024-04-27 02:32:57
合計ジャッジ時間 936 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.8.2/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )

Main.hs:24:3: error: [GHC-39999]
    • No instance for ‘MonadFail Data.Functor.Identity.Identity’
        arising from a do statement
        with the failable pattern ‘(x : xs)’
    • In a stmt of a 'do' block: (x : xs) <- get
      In the expression:
        do (x : xs) <- get
           case x of
             '+' -> do ...
             _ -> do ...
      In an equation for ‘op’:
          op
            = do (x : xs) <- get
                 case x of
                   '+' -> ...
                   _ -> ...
   |
24 |   (x:xs) <- get
   |   ^^^^^^^^^^^^^

ソースコード

diff #

import Control.Applicative ((<$>))
import Data.Char (isDigit)
import Control.Monad.State

main :: IO ()
main = solve <$> getLine >>= print

solve :: String -> Int
solve = evalState expr

num :: State String Int
num = do
  ss@(x:xs) <- get
  case x of
   '+' -> do put $ dropWhile isDigit xs
             return . read . takeWhile isDigit $ xs
   '-' -> do put $ dropWhile isDigit xs
             return . negate . read . takeWhile isDigit $ xs
   _ -> do put $ dropWhile isDigit ss
           return . read . takeWhile isDigit $ ss

op :: State String (Int -> Int -> Int)
op = do
  (x:xs) <- get
  case x of
   '+' -> do put xs
             return (-)
   _ -> do put xs
           return (+)

expr :: State String Int
expr = do
  x <- num
  f <- op
  y <- num
  return $ f x y
0