diff --git a/app/Main.hs b/app/Main.hs index 6e170ef..f3808bf 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,24 +1,64 @@ module Main where -import Lib -import MyGetOpt +--import MyGetOpt -data Cell = Empty | Full -newType CellList = CellList { left :: [], middle :: [], right :: [] } +data Cell = Empty | Full + deriving (Read, Show, Eq, Ord, Enum, Bounded) +type CellList = [Cell] +type CellGen = Cell -> Cell -> Cell -> Cell + +rule30 :: Cell -> Cell -> Cell -> Cell +rule30 Full Empty Empty = Full +rule30 Empty Full Full = Full +rule30 Empty Full Empty = Full +rule30 Empty Empty Full = Full +rule30 _ _ _ = Empty + +getrule :: Int -> CellGen +getrule 30 = rule30 +getrule r = error $ "Unsupported rule" ++ (show r) +--getrule 90 = rule90 +--getrule 110 = rule110 run :: Int -> [CellList] -run _ = [] +run ri = iterate generate [Full] + where + r = getrule ri + + generate :: CellList -> CellList + generate [] = [] + generate (x:[]) = r Empty Empty x : r Empty x Empty : r x Empty Empty : [] + generate (x:y:xs) = r Empty Empty x : r Empty x y : endgen (x:y:xs) + + endgen :: CellList -> CellList + endgen (x:y:z:xs) = r x y z : endgen (y:z:xs) + endgen (x:y:[]) = r x y Empty : r y Empty Empty : [] + endgen _ = error "Invalid generator status" printCells :: Configuration -> [CellList] -> IO() -printCells _ [] = return +printCells _ [] = putChar '\n' printCells config (x:xs) = pl x >> printCells config xs where - pl cl = + pl :: CellList -> IO() + pl [] = putChar '\n' + pl (y:ys) = putChar (if y == Empty then ' ' else '*') >> pl ys main :: IO () -main = do - config = Configuration 30 0 10 80 0 - printCells config $ run (rule config) +main = case getConfig of + Nothing -> exitFailure(ExitCode 84) + Just config -> runAndPrint config + where + runAndPrint :: Configuration -> IO() + runAndPrint config = printCells config (post . run (rule config)) + + post :: [CellList] -> [CellList] + post = skipX (start config) . takeX () + + let cells = run (rule config) + let fcells = case Main.lines config of + Nothing -> cells + Just x -> take x cells + printCells config fcells -- print option -- ( long "rule" -- <> short "r" @@ -26,6 +66,9 @@ main = do -- <> help "The rulset used." -- ) +getConfig :: Maybe Configuration +getConfig = Just Configuration 30 (Just 0) (Just 10) (Just 80) (Just 0) + data Configuration = Configuration { rule :: Int, diff --git a/src/MyGetOpt.hs b/src/MyGetOpt.hs index abfd39d..d4c5b42 100644 --- a/src/MyGetOpt.hs +++ b/src/MyGetOpt.hs @@ -3,7 +3,7 @@ module MyGetOpt where import Control.Applicative import Data.Char -newtype Parser a = Parser { parse :: (String -> Maybe (a, String)) } +newtype Parser a = Parser { parse :: String -> Maybe (a, String) } instance Functor Parser where -- fmap :: (a -> b) -> f a -> f b @@ -29,7 +29,7 @@ instance Monad Parser where instance Alternative Parser where -- empty :: f a - empty = Parser (\_ -> Nothing) + empty = Parser $ const Nothing -- (<|>) :: f a -> f a -> f a (<|>) a b = Parser $ \x -> case parse a x of diff --git a/wolfram.iml b/wolfram.iml new file mode 100644 index 0000000..b62eae0 --- /dev/null +++ b/wolfram.iml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file