From 2829f5999e1f58bd8f2f7bc1d020bfdb8b9429c0 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan Date: Sun, 28 Dec 2014 13:37:21 +0530 Subject: [PATCH] hw11: exercise 1 --- hw11/SExpr.hs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) 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 -- 2.37.2