Skip to content

Instantly share code, notes, and snippets.

@angerman

angerman/Lib.hs Secret

Created September 1, 2017 03:22
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 angerman/c6ee51e4892ce6efdbcabb8c5ab990fa to your computer and use it in GitHub Desktop.
Save angerman/c6ee51e4892ce6efdbcabb8c5ab990fa to your computer and use it in GitHub Desktop.
name: break
version: 0.1.0.0
build-type: Simple
cabal-version: >=1.10
library
exposed-modules: Lib
build-depends: base >=4.10 && <4.11
, transformers
hs-source-dirs: src
default-language: Haskell2010
executable test
main-is: Main.hs
build-depends: base
, break
hs-source-dirs: exe
default-langauge: Haskell2010
module Lib where
import Control.Monad.Trans.State.Strict
eval :: Int -> State Int a -> a
eval p = fst . flip runState p
advance :: Int -> State Int ()
advance = modify' . (+)
loc :: State Int Int
loc = get
emit1 :: State Int ()
emit1 = advance 1
emitN :: Int -> State Int ()
-- adding in the 0 case, breaks with HEAD. 8.2.1 is fine with it.
-- emitN 0 = advance 0
emitN 0 = pure ()
emitN n = advance n
align8 :: State Int ()
align8 = do
bits <- (`mod` 8) <$> loc
emitN (8 - bits)
module Main where
import Lib
import System.Exit
main :: IO ()
main = do
let p = eval 0 (emit1 >> align8 >> loc)
putStrLn $ show p
if p == 8
then putStrLn "OK" >> exitSuccess
else putStrLn "FAIL" >> exitFailure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment