import Data.Char import Control.Applicative newtype Parser a = P {rodaParser :: String -> Maybe (a, String)} satisfaz :: (Char -> Bool) -> Parser Char satisfaz condição = P f where f (x:xs) = if condição x then Just (x, xs) else Nothing f [] = Nothing caracter :: Char -> Parser Char caracter c = satisfaz (== c) intPos :: Parser Integer intPos = P f where f x = case span isDigit x of ("", _) -> Nothing (str_nums, resto) -> Just (read str_nums, resto) {- span condição lista = um par de listas, a primeira sendo o "segmento inicial" dos elementos da lista que satisfazem a condição, e a segunda o restante isDigit é de Data.Char -} ---------------------- -- Q3a instance Functor Parser where -- fmap :: (a -> b) -> Parser a -> Parser b fmap = undefined ---------------------- -- Q3b instance Applicative Parser where -- pure :: a -> Parser a pure = undefined -- (<*>) :: Parser (a -> b) -> Parser a -> Parser b (<*>) = undefined ---------------------- -- Q3c instance Alternative Parser where -- empty :: Parser a empty = undefined -- (<|>) :: Parser a -> Parser a -> Parser a (<|>) = undefined ---------------------- -- Q3d zeroOuMais :: Parser a -> Parser [a] zeroOuMais = undefined umOuMais :: Parser a -> Parser [a] umOuMais = undefined ---------------------- -- Q3e parseEspaços :: Parser String parseEspaços = undefined ---------------------- -- Q3f type Identif = String parseIdentif :: Parser Identif parseIdentif = undefined ---------------------- -- Q3g data Átomo = N Integer | I Identif deriving (Eq, Show) parseÁtomo :: Parser Átomo parseÁtomo = undefined ---------------------- -- Q3h data SExpr = A Átomo | C [SExpr] deriving (Eq, Show) parseSExpr :: Parser SExpr parseSExpr = undefined