, . , , : , .
"string" "bit-string",
newtype Parser a = Parser (StateT [Bool] [] a)
deriving (Functor, Applicative, Monad, Alternative)
runParser :: Parser a -> [Bool] -> [(a, [Bool])]
runParser (Parser m) = runStateT m
, , a s. GHC GeneralizedNewtypeDeriving Monad .
, Parser (Tree SevenBits) - , . ( 7 Word8 Functor Tree fmap.) Tree, - , , .
data Tree a = Leaf a | Node (Tree a) (Tree a) deriving Show
type SevenBits = (Bool, Bool, Bool, Bool, Bool, Bool, Bool)
, , :
one :: Parser Bool
one = Parser $ do
stream <- get
case stream of
[] -> empty
(x:xs) -> put xs *> return x
, , :
bit :: Bool -> Parser ()
bit b = do
i <- one
guard (i == b)
, replicateM . Leaf.
sevenBits :: Parser SevenBits
sevenBits = pack7 <$> replicateM 7 one
where pack7 [a,b,c,d,e,f,g] = (a, b, c, d, e, f, g)
, , , . Node Leaf, <|>.
tree :: Parser (Tree SevenBits)
tree = node <|> leaf
where node = bit False *> liftA2 Node tree tree
leaf = bit True *> fmap Leaf sevenBits
Node , , , liftA2. , Node , , <|> Node Leaf.
, Tree Tree. . , one , case , , . -, , .
@behzad.nouri foldr. , , - - , , liftA2 <|>, , .
, , Leaf, (-) 0 1. , .
ghci> runParser tree $ map (>0) [0, 1, 0,0,0,0,0,0,0, 1, 0,0,0,0,0,0,1]
[(Node (Leaf (False, False, False, False, False, False, False)) (Leaf (False, False, False, False, False, False, True)),[])]