-
Notifications
You must be signed in to change notification settings - Fork 0
/
february-1-parser-pratul.hs
58 lines (47 loc) · 1.98 KB
/
february-1-parser-pratul.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module February1Parser where
import Data.Char
import Debug.Trace
newtype Parser i o = Parser { runParser :: i -> Maybe (o, i) }
instance Functor (Parser i) where
-- fmap :: (o -> o') -> Parser i o -> Parser i o'
fmap f p = Parser $ \input ->
let Maybe (o, rest) = runParser p input
in (fmap f mo, rest)
instance Applicative (Parser i) where
pure x = Parser $ \input -> (Just x, input)
-- (<*>) :: Parser i (o -> o') -> Parser i o -> Parser i o'
pf <*> po = Parser $ \input -> case runParser pf input of
(Nothing, _) -> (Nothing, input)
(Just f, rest) -> case runParser po rest of
(Nothing, _) -> (Nothing, rest)
(Just o, rest') -> (Just (f o), rest')
type Digit = Int
digit :: Parser String Digit
digit = Parser $ \input -> traceShow input $ traceShowId $ case input of
(c:cs) | isDigit c -> (Just $ digitToInt c, cs)
cs -> (Nothing, cs)
wholeNumber :: Parser String Int
wholeNumber = pure (\d num -> d * 10^countDigits num + num)
<*> digit
<*> wholeNumber
where
countDigits = length . show
-- wholeNumber = Parser $ \input -> case input of
-- "" -> (Nothing, "")
-- _ -> case runParser digit input of
-- (Just d, rest) -> case runParser wholeNumber rest of
-- (Just num, rest') -> (Just (d * 10^countDigits num + num), rest')
-- (Nothing, rest') -> (Just d, rest')
-- (Nothing, rest) -> (Nothing, rest)
--
char :: Char -> Parser String Char
char x = Parser $ \input -> case input of
(c:cs) | c == x -> (Just c, cs)
_ -> (Nothing, input)
word :: String -> Parser String String
word "" = Parser $ \input -> (Just "", input)
word (x:xs) = Parser $ \input -> case runParser (char x) input of
(Just c, rest) -> case runParser (word xs) rest of
(Just w, rest') -> (Just (c:w), rest')
(Nothing, _) -> (Nothing, input)
(Nothing, rest) -> (Nothing, rest)