Created
June 26, 2014 12:10
-
-
Save lnicola/09328395db1689a4f4b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Control.Monad | |
| data T = L | B T T | |
| instance Show T where | |
| show L = "L" | |
| show (B left right) = 'B' : (show left ++ show right) | |
| newtype Parser a = Parser (String -> (a, String)) | |
| parse (Parser p) = p | |
| instance Monad Parser where | |
| return a = Parser $ \cs -> (a, cs) | |
| m >>= f = Parser $ \cs -> let (a, cs') = parse m cs in parse (f a) cs' | |
| char = Parser $ \(c : cs) -> (c, cs) | |
| parseTree = do | |
| c <- char | |
| case c of | |
| 'L' -> return L | |
| 'B' -> liftM2 B parseTree parseTree | |
| {- | |
| 'B' -> do | |
| left <- parseTree | |
| right <- parseTree | |
| return $ B left right | |
| -} | |
| main = print $ parse parseTree "BBLLL" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment