Skip to content

Commit 01db992

Browse files
committed
initial commit
0 parents  commit 01db992

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

main.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import System.Environment
2+
import Debug.Trace
3+
4+
5+
main = do
6+
args <- getArgs
7+
mapM_ putStrLn args

parser.hs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{-# LANGUAGE LambdaCase #-}
2+
module Parser where
3+
import Control.Monad
4+
5+
-- | empty list of of results denotes failure
6+
newtype Parser a = Parser {
7+
-- |
8+
parse :: String -> [(a, String)] }
9+
10+
11+
item :: Parser Char
12+
item = Parser (\case
13+
"" -> []
14+
(c:cs) -> [(c,cs)])
15+
16+
instance Functor Parser where
17+
fmap = liftM
18+
19+
instance Applicative Parser where
20+
pure = return
21+
(<*>) = ap
22+
23+
instance Monad Parser where
24+
return a = Parser (\cs -> [(a,cs)])
25+
p >>= f = Parser (\cs -> concat [parse (f a) cs' | (a,cs') <- parse p cs])
26+
27+
-- | consume three chars, throw away 2nd and return tuple of 1st and 3rd char
28+
p :: Parser (Char,Char)
29+
p = do { c <- item; item; d <- item; return (c,d); }

0 commit comments

Comments
 (0)