File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import System.Environment
2+ import Debug.Trace
3+
4+
5+ main = do
6+ args <- getArgs
7+ mapM_ putStrLn args
Original file line number Diff line number Diff line change 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); }
You can’t perform that action at this time.
0 commit comments