Created
October 23, 2019 08:42
-
-
Save gdeest/ecd82c83683bd0cf6d8454176e6859b6 to your computer and use it in GitHub Desktop.
This file contains 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
data Maybe a = Nothing | Just a | |
data Expr = SumExpr Expr Expr | |
| ProdExpr Expr Expr | |
| Power Expr Expr | |
| Val Float | |
String -> Maybe Expr | |
-- "1+2*3" → Just (SumExpr (Val 1.0) (ProdExpr (Val 2.0) (Val 3.0)) | |
-- "1+(2*3" → Nothing | |
-- | |
data Cmd = GET | POST | PUT | DELETE | |
parseCmd :: Parser Cmd | |
:: String -> Maybe (Cmd, String) | |
parseCmd "GET123" → Just (GET, "123") | |
parseCmd "Robert123" → Nothing | |
type String = [Char] | |
[] | |
(x:xs) | |
"GET" = 'G':('E':'T:[]) | |
parseCmd ('G':('E':('T':rest))) = Just (GET, rest) | |
parseCmd ('P':('U':('T':rest))) = Just (PUT, rest) | |
parseCmd ('P':('O':('S':('T':rest)))) = Just (POST, rest) | |
parseCmd ('D':('E':('L':('E':('T':('E':rest)))))) = Just (DELETE, rest) | |
parseCmd _ = Nothing | |
-- | |
type Parser a = String → Maybe (a, String) | |
parseString :: a -> String -> Parser a | |
:: a -> String -> (String -> Maybe (a, String)) | |
parseString a "" input = Just (a, input) | |
-- ^ Même chose que: parseString a "" = \input -> Just (a, input) | |
parseString a (x:xs) "" = Nothing | |
parseString a (x:xs) (y:ys) | x == y = parseString a xs ys | |
parseString a _ _ = Nothing | |
parseGet :: Parser Cmd | |
parseGet = parseString GET "GET" | |
parsePost = … | |
parsePut = … | |
anyOf :: [Parser a] -> Parser a | |
-- anyOf :: [String -> Maybe (a, String)] -> (String -> Maybe (a, String)) | |
anyOf [] _ = Nothing | |
anyOf (p:ps) input = | |
case p input of | |
Nothing -> anyOf ps input | |
ret -> ret | |
parseCmd :: Parser Cmd | |
parseCmd = anyOf [parseGet, parsePost, parsePut, parseDelete] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment