Skip to content

Instantly share code, notes, and snippets.

@kakkun61
Last active February 4, 2018 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kakkun61/4bbc966ede75c98109fd2c136dc0499a to your computer and use it in GitHub Desktop.
Save kakkun61/4bbc966ede75c98109fd2c136dc0499a to your computer and use it in GitHub Desktop.
手続き Haskell
module Main where
import Prelude hiding (break)
import Control.Monad (when)
import Control.Monad.Cont (callCC, ContT, runContT)
import Control.Monad.IO.Class (liftIO)
import qualified Data.Foldable
import Data.IORef (newIORef, readIORef, writeIORef)
import qualified Data.Traversable
main :: IO ()
main = do
nRef <- newIORef (0 :: Int)
for_ [1 .. 9] $ \i break -> do
n <- liftIO $ readIORef nRef
let n' = n + i
when (n' > 40) break
liftIO $ writeIORef nRef n'
n <- readIORef nRef
print n
for_ :: (Foldable t, Applicative f) => t a -> (a -> ContT () f () -> ContT () f ()) -> f ()
for_ t f = flip runContT pure $ callCC $ \cc -> Data.Foldable.for_ t (flip f $ cc ())
for :: (Traversable t, Applicative f) => t a -> (a -> (t b -> ContT (t b) f c) -> ContT (t b) f b) -> f (t b)
for t f = flip runContT pure $ callCC $ \cc -> Data.Traversable.for t (flip f cc)
name: procedural
version: 0.1.0.0
-- synopsis:
-- description:
-- homepage:
license: BSD3
-- license-file: LICENSE
author: Kazuki Okamoto
maintainer: kazuki.okamoto@kakkun61.com
copyright: 2018 Kazuki Okamoto
-- category:
build-type: Simple
-- extra-source-files:
cabal-version: >=1.10
executable procedural
hs-source-dirs: .
main-is: Main.hs
ghc-options: -threaded
-rtsopts
-with-rtsopts=-N
-Wall
-Wincomplete-uni-patterns
-Wincomplete-record-updates
-Wmonomorphism-restriction
-Wimplicit-prelude
-Wmissing-local-signatures
-Wmissing-exported-signatures
-Wmissing-import-lists
-Wmissing-home-modules
-Widentities
-Wredundant-constraints
-Wno-implicit-prelude
build-depends: base
, mtl
default-language: Haskell2010
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment