-
-
Save angerman/c6ee51e4892ce6efdbcabb8c5ab990fa to your computer and use it in GitHub Desktop.
This file contains 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
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 |
This file contains 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
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) |
This file contains 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
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