From: Ramakrishnan Muthukrishnan Date: Sun, 28 Dec 2014 08:07:21 +0000 (+0530) Subject: hw11: exercise 1 X-Git-Url: https://git.rkrishnan.org/pf/content/en/seg/priv/something?a=commitdiff_plain;h=2829f5999e1f58bd8f2f7bc1d020bfdb8b9429c0;p=yorgey.git hw11: exercise 1 --- diff --git a/hw11/SExpr.hs b/hw11/SExpr.hs index 78ae980..c3f7d70 100644 --- a/hw11/SExpr.hs +++ b/hw11/SExpr.hs @@ -10,12 +10,19 @@ import Control.Applicative ------------------------------------------------------------ -- 1. Parsing repetitions ------------------------------------------------------------ +sequenceA :: Applicative f => [f a] -> f [a] +sequenceA [] = pure [] +sequenceA (x:xs) = (:) <$> x <*> sequenceA xs +-- sequenceA (x:xs) = liftA2 (:) x (sequenceA xs) + +replicateA :: Applicative f => Int -> f a -> f [a] +replicateA n x = sequenceA (replicate n x) zeroOrMore :: Parser a -> Parser [a] -zeroOrMore p = undefined +zeroOrMore p = oneOrMore p <|> pure [] oneOrMore :: Parser a -> Parser [a] -oneOrMore p = undefined +oneOrMore p = liftA2 (:) p (zeroOrMore p) ------------------------------------------------------------ -- 2. Utilities