Skip to content

Instantly share code, notes, and snippets.

@stmtk1
Last active January 16, 2020 01:39
Show Gist options
  • Save stmtk1/5667bd1d8ee612217d4094d18a62ef6d to your computer and use it in GitHub Desktop.
Save stmtk1/5667bd1d8ee612217d4094d18a62ef6d to your computer and use it in GitHub Desktop.
module Main where
import System.Random
import Control.Monad.State.Strict
main = do
putStrLn $ evalState ((loop 100) =<< genCell 100) (mkStdGen 1)
newtype Cell = Cell { unwrapCell :: [Bool] }
genBool :: State StdGen Bool
genBool = state random
genBoolList :: Int -> State StdGen [Bool]
genBoolList n = replicateM n genBool
genCell :: Int -> State StdGen Cell
genCell n = Cell <$> genBoolList n
appendEdge :: [Bool] -> [Bool] -> [Bool]
appendEdge [fst, lst] c = fst : c ++ [lst]
appendedCell :: Cell -> State StdGen [Bool]
appendedCell cell = (\t -> appendEdge t (unwrapCell cell)) <$> replicateM 2 genBool
nextStateList :: [Bool] -> [Bool]
nextStateList (a:b:c:xs) = (nextStateBool a b c) : (nextStateList (b:c:xs))
nextStateList _ = []
nextStateBool :: Bool -> Bool -> Bool -> Bool
nextStateBool False False False = False
nextStateBool False False True = False
nextStateBool False True False = False
nextStateBool False True True = True
nextStateBool True False False = True
nextStateBool True False True = True
nextStateBool True True False = False
nextStateBool True True True = True
nextState :: Cell -> State StdGen Cell
nextState cell = ((\t -> Cell t) . nextStateList) <$> appendedCell cell
loop :: Int -> Cell -> State StdGen String
loop 0 cell = pure $ show cell
loop n cell = (\t -> (show cell) ++ "\n" ++ t) <$> ((loop (n - 1)) =<< nextState cell)
instance Show Cell where
show cell = map (\t -> if t then '+' else '-') $ unwrapCell cell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment