Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Block6.Task4
- ( listlistParser
- ) where
- import Block6.Task1
- import Block6.Task2 (element, satisfy)
- import Block6.Task3 (parseNumber)
- import Control.Applicative (many, (<|>))
- import Prelude hiding (cycle)
- import Text.Read (readMaybe)
- listlistParser :: Parser Char [[Int]]
- listlistParser =
- Parser $ \s -> do
- (len, rest) <- runParser cycleOrNumber s
- runParser (add (exactly len) (listlistParser <|> eof)) rest
- spaces :: Parser Char [Char]
- spaces = many $ element ' '
- comma :: Parser Char Char
- comma = element ','
- exactly :: Int -> Parser Char [Int]
- exactly n =
- Parser $ \s ->
- if n == 0
- then Just ([], s)
- else runParser (add cycleOrNumber (exactly $ n - 1)) s
- cycle :: Parser Char Int
- cycle = spaces *> comma *> spaces *> parseNumber
- cycleOrNumber :: Parser Char Int
- cycleOrNumber = cycle <|> parseNumber
- add :: Parser s a -> Parser s [a] -> Parser s [a]
- add p res =
- Parser $ \s -> do
- (first, rest) <- runParser p s
- (other, t) <- runParser res rest
- return ([first] ++ other, t)
- eof :: Parser s [[Int]]
- eof =
- Parser $ \s ->
- case s of
- [] -> Just ([[]], [])
- _ -> Nothing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement