]> git.rkrishnan.org Git - yorgey.git/blobdiff - hw10/AParser.hs
hw10: exercises 4 and 5
[yorgey.git] / hw10 / AParser.hs
index f506fcb1589e2395992be0c5459253b7cbfa0cfc..04eb6ad0de6bd4c7cc1ad492e2c68cfd7ff58e74 100644 (file)
@@ -96,3 +96,17 @@ abParser_ = (\_ _ -> ()) <$> char 'a' <*> char 'b'
 
 intPair :: Parser [Integer]
 intPair = (\x _ z -> x : z : []) <$> posInt <*> char ' ' <*> posInt
+
+-- Exercise 4
+-- class Applicative f => Alternative f where
+--     empty :: f a
+--     (<|>) :: f a -> f a -> f a
+instance Alternative Parser where
+    empty = Parser(\_ -> Nothing)
+    (Parser p1) <|> (Parser p2) = Parser (\inp -> case (p1 inp) of
+                                                    Nothing -> (p2 inp)
+                                                    Just v -> Just v)
+
+-- exercise 5: int or Uppercase
+intOrUppercase :: Parser ()
+intOrUppercase = ((\_ -> ()) <$> posInt) <|> ((\_ -> ()) <$> (satisfy isUpper))