Last active
July 27, 2018 05:25
-
-
Save joshuakfarrar/dcaa77124f08367f118ea347b3168d5f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{- | |
State monad things, but in Haskell. | |
I am but a humble FP noob. | |
-} | |
module Main where | |
import Control.Monad.State | |
type GameState = (Bool, Int) | |
-- convenience type | |
-- a limitations is that when there are multiple value types, these "convenience" | |
-- types can become numerous and less convenient | |
type Transform = State GameState GameState | |
addSeventeen :: Transform | |
addSeventeen = do | |
(on, n) <- get | |
return (on, n + 17) | |
addOne :: Transform | |
addOne = do | |
(on, n) <- get | |
return (on, n + 1) | |
inputToTransform :: Bool -> Transform | |
inputToTransform x = | |
case x of | |
True -> addSeventeen | |
False -> addOne | |
applyTransforms :: [Transform] -> Transform | |
applyTransforms inputs = do | |
initialState <- get | |
return $ foldl (\state input -> evalState input state) initialState inputs | |
-- point-free! | |
transform :: [Bool] -> Transform | |
transform = (applyTransforms . map inputToTransform) | |
main :: IO () | |
main = do | |
let game = (True, 0) | |
let inputs = [True, False, False, False] | |
print $ runState (transform inputs) game |
This file contains hidden or 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: similar | |
version: 0.1.0.0 | |
-- synopsis: | |
-- description: | |
license: BSD3 | |
license-file: LICENSE | |
-- copyright: | |
-- category: | |
build-type: Simple | |
extra-source-files: ChangeLog.md | |
cabal-version: >=1.10 | |
executable lol-woops | |
main-is: Main.hs | |
-- other-modules: | |
-- other-extensions: | |
build-depends: base >=4.10 && <4.11, mtl | |
hs-source-dirs: . | |
default-language: Haskell2010 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment