mirror of
https://github.com/zoriya/applicative-getopt.git
synced 2026-05-31 01:55:47 +00:00
Working on the project
This commit is contained in:
+53
-10
@@ -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,
|
||||
|
||||
+2
-2
@@ -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
|
||||
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="HASKELL_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/app" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/.stack-work" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="base-4.13.0.0" level="project" />
|
||||
<orderEntry type="library" name="ghc-prim-0.5.3" level="project" />
|
||||
<orderEntry type="library" name="integer-gmp-1.0.2.0" level="project" />
|
||||
</component>
|
||||
</module>
|
||||
Reference in New Issue
Block a user