Parsing
Algorithms
Strings
Data Structures
Functional Programming
Code
Diff
• ``````{-# LANGUAGE DeriveFunctor, TupleSections, LambdaCase #-}

import Control.Applicative

newtype Parser a = Parser { runParser :: String -> [(a, String)] }
deriving Functor

instance Applicative Parser where
pure a = Parser \$ pure . (a,)
(<*>) = ap

return = pure
Parser p >>= f = Parser \$ \s -> concatMap (uncurry (runParser.f)) \$ p s

instance Alternative Parser where
empty = Parser \$ pure []
Parser p <|> Parser q = Parser \$ liftA2 (++) p q

pred1 :: (Char -> Bool) -> Parser Char
pred1 p = Parser \$ \case c:cs | p c -> [(c, cs)]; _ -> []

char :: Char -> Parser Char
char = pred1 . (==)

str :: String -> Parser String
str = mapM char
``````
•  1 − ```{-# LANGUAGE DeriveFunctor, TupleSections #-} ``` 1 + ```{-# LANGUAGE DeriveFunctor, TupleSections, LambdaCase #-} ``` 2 2 ```module MonadicParsing where ``` 3 3 4 + ```import Control.Applicative ``` 5 + ```import Control.Monad ``` 6 + 4 4 ```newtype Parser a = Parser { runParser :: String -> [(a, String)] } ``` 5 5 ``` deriving Functor ``` 6 − ``` ``` 9 + 10 + ```instance Applicative Parser where ``` 11 + ``` pure a = Parser \$ pure . (a,) ``` 12 + ``` (<*>) = ap ``` 13 + 7 7 ```instance Monad Parser where ``` 8 − ``` return a = Parser \$ return . (a,) ``` 15 + ``` return = pure ``` 9 9 ``` Parser p >>= f = Parser \$ \s -> concatMap (uncurry (runParser.f)) \$ p s ``` 17 + 18 + ```instance Alternative Parser where ``` 19 + ``` empty = Parser \$ pure [] ``` 20 + ``` Parser p <|> Parser q = Parser \$ liftA2 (++) p q ``` 21 + 22 + 23 + ```pred1 :: (Char -> Bool) -> Parser Char ``` 24 + ```pred1 p = Parser \$ \case c:cs | p c -> [(c, cs)]; _ -> [] ``` 25 + 26 + ```char :: Char -> Parser Char ``` 27 + ```char = pred1 . (==) ``` 28 + 29 + ```str :: String -> Parser String ``` 30 + ```str = mapM char ```