File tree 1 file changed +15
-1
lines changed
1 file changed +15
-1
lines changed Original file line number Diff line number Diff line change @@ -35,6 +35,15 @@ instance Monad Parser where
35
35
[] -> []
36
36
[(value, out)] -> parse (functionToParser value) out)
37
37
38
+ instance Alternative Parser where
39
+ -- empty :: Parser a
40
+ empty = P (\ inp -> [] )
41
+
42
+ -- (<|>) :: Parser a -> Parser a -> Parser a
43
+ p <|> q = P (\ inp -> case parse p inp of
44
+ [] -> parse q inp
45
+ [(v, out)] -> [(v, out)])
46
+
38
47
parse :: Parser a -> String -> [(a , String )]
39
48
parse (P p) inp = p inp
40
49
@@ -74,4 +83,9 @@ main = do
74
83
putStrLn " Monad:"
75
84
putStrLn . show $ parse (return ' 1' ) " abc"
76
85
putStrLn . show $ parse (item >>= (\ x -> crappyParser)) " abcdef"
77
- putStrLn . show $ parse (item >>= uselessParser) " abcdef"
86
+ putStrLn . show $ parse (item >>= uselessParser) " abcdef"
87
+
88
+ putStrLn " Alternative:"
89
+ putStrLn . show $ ((parse empty " abc" ) :: [(String , String )])
90
+ putStrLn . show $ parse (item <|> return ' d' ) " abc"
91
+ putStrLn . show $ parse (empty <|> return ' d' ) " abc"
You can’t perform that action at this time.
0 commit comments