1

計算を一時停止し、後で要求に応じて (ユーザーからのプロンプトで) 再開しようとしています。継続モナドのみを使用すると、次のようになります。

import Control.Monad.IO.Class (liftIO,MonadIO(..))
import Data.Void

f :: MonadIO m => Int -> Int -> m Void
f x y = do
  liftIO (print x)
  input <- liftIO getLine
  if input /= "pause"
     then (f (x+1) y)
     else (f' y x)

f' :: MonadIO m => Int -> Int -> m Void
f' x y = do
  liftIO (print x)
  input <- liftIO getLine
  if input /= "pause"
     then (f' (x-1) y)
     else (f y x)

出力例:

λ> f 5 5
5

6

7

8
pause
5

4

3

2
pause
8

9

10
Interrupted.

質問の元のバージョン:

-- Helpers
cond = fmap (not . (== "pause")) getLine
ch b x y = if b then x else y
ch' :: a -> a -> Bool -> a
ch' = flip . (flip ch)

-- Main code
f i i' = liftIO (print i) >> liftIO cond >>= ch' (f (i+1) i') (f' i'  i)
f' i i' = liftIO (print i) >> liftIO cond >>= ch' (f' (i-1) i') (f i' i)
4

0 に答える 0