Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import System.Environment
  2. import System.Exit
  3. import Data.List
  4.  
  5. myElem :: Eq a => a -> [a] -> Bool
  6. myElem a [] = False
  7. myElem a (b:bs)
  8.     | a == b = True
  9.     | otherwise = myElem a bs
  10.  
  11. checkIfInt :: [Char] -> Bool
  12. checkIfInt [] = False
  13. checkIfInt [a] = case myElem a ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] of
  14.     True -> True
  15.     False -> False
  16. checkIfInt (a:as) = case myElem a ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] of
  17.     True -> checkIfInt as
  18.     False -> False
  19.  
  20. checkIfIntWithNegative :: [Char] -> Bool
  21. checkIfIntWithNegative [] = False
  22. checkIfIntWithNegative [a] = case myElem a ['-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] of
  23.     True -> True
  24.     False -> False
  25. checkIfIntWithNegative (a:as) = case myElem a ['-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] of
  26.     True -> checkIfIntWithNegative as
  27.     False -> False
  28.  
  29. printElements :: [String] -> IO()
  30. printElements [] = return ()
  31. printElements (x:xs) = do putStrLn x
  32.                           printElements xs
  33.  
  34. getNextArgs :: [String] -> String
  35. getNextArgs [] = "Empty"
  36. getNextArgs (x:xs) = x
  37.  
  38. parseArgs :: [String] -> String -> String
  39. parseArgs [] b = "Empty"
  40. parseArgs (x:xs) b = case x == b of
  41.     True -> getNextArgs xs
  42.     False -> do
  43.         parseArgs xs b
  44.  
  45. checkRule :: [String] -> IO String
  46. checkRule arguments = do
  47.     let rule = parseArgs arguments "--rule"
  48.     let is_arg_int = checkIfInt rule
  49.     if (is_arg_int == False)
  50.         then do
  51.             putStrLn ("No rule set")
  52.             exitWith (ExitFailure 84)
  53.         else
  54.             return rule
  55.  
  56. checkStart :: [String] -> IO String
  57. checkStart arguments = do
  58.     let start = parseArgs arguments "--start"
  59.     if start == "Empty"
  60.         then return ("0")
  61.     else do
  62.         let is_arg_int = checkIfInt start
  63.         if (is_arg_int == False)
  64.             then do
  65.                 putStrLn ("Start is not an integer")
  66.                 exitWith (ExitFailure 84)
  67.             else
  68.                 return start
  69.  
  70. checkLine :: [String] -> IO String
  71. checkLine arguments = do
  72.     let lines = parseArgs arguments "--lines"
  73.     if lines == "Empty"
  74.         then return ("0")
  75.     else do
  76.         let is_arg_int = checkIfInt lines
  77.         if (is_arg_int == False)
  78.             then do
  79.                 putStrLn ("Lines is not an integer")
  80.                 exitWith (ExitFailure 84)
  81.             else
  82.                 return lines
  83.  
  84. checkWindow :: [String] -> IO String
  85. checkWindow arguments = do
  86.     let window = parseArgs arguments "--window"
  87.     if window == "Empty"
  88.         then return ("80")
  89.     else do
  90.         let is_arg_int = checkIfInt window
  91.         if (is_arg_int == False)
  92.             then do
  93.                 putStrLn ("Window is not an integer")
  94.                 exitWith (ExitFailure 84)
  95.             else
  96.                 return window
  97.  
  98. checkMove :: [String] -> IO String
  99. checkMove arguments = do
  100.     let move = parseArgs arguments "--move"
  101.     if move == "Empty"
  102.         then return ("80")
  103.     else do
  104.         let is_arg_int = checkIfInt move
  105.         if (is_arg_int == False)
  106.             then do
  107.                 putStrLn ("Move is not an integer")
  108.                 exitWith (ExitFailure 84)
  109.             else
  110.                 return move
  111.  
  112. printSpaces :: Int -> IO()
  113. printSpaces 0 = putStr(" ")
  114. printSpaces x = do
  115.     putStr(" ")
  116.     printSpaces (x-1)
  117.  
  118. main = do
  119.     arguments <- getArgs
  120.     rules <- checkRule arguments
  121.     start <- checkStart arguments
  122.     lines <- checkLine arguments
  123.     window <- checkWindow arguments
  124.     move <- checkMove arguments
  125.     printSpaces 80
  126.     return 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement