mirror of
https://github.com/sdiehl/write-you-a-haskell.git
synced 2024-08-16 23:20:54 +03:00
Merge pull request #65 from sgraf812/master
Added Alternative constraint and made some minor changes
This commit is contained in:
commit
1e609fbff1
@ -105,14 +105,14 @@ itself if there is not at least a single match.
|
|||||||
|
|
||||||
```haskell
|
```haskell
|
||||||
-- | One or more.
|
-- | One or more.
|
||||||
some :: f a -> f [a]
|
some :: Alternative f => f a -> f [a]
|
||||||
some v = some_v
|
some v = some_v
|
||||||
where
|
where
|
||||||
many_v = some_v <|> pure []
|
many_v = some_v <|> pure []
|
||||||
some_v = (:) <$> v <*> many_v
|
some_v = (:) <$> v <*> many_v
|
||||||
|
|
||||||
-- | Zero or more.
|
-- | Zero or more.
|
||||||
many :: f a -> f [a]
|
many :: Alternative f => f a -> f [a]
|
||||||
many v = many_v
|
many v = many_v
|
||||||
where
|
where
|
||||||
many_v = some_v <|> pure []
|
many_v = some_v <|> pure []
|
||||||
|
@ -12,7 +12,7 @@ runParser :: Parser a -> String -> a
|
|||||||
runParser m s =
|
runParser m s =
|
||||||
case parse m s of
|
case parse m s of
|
||||||
[(res, [])] -> res
|
[(res, [])] -> res
|
||||||
[(_, rs)] -> error "Parser did not consume entire stream."
|
[(_, _)] -> error "Parser did not consume entire stream."
|
||||||
_ -> error "Parser error."
|
_ -> error "Parser error."
|
||||||
|
|
||||||
item :: Parser Char
|
item :: Parser Char
|
||||||
@ -62,7 +62,7 @@ satisfy :: (Char -> Bool) -> Parser Char
|
|||||||
satisfy p = item `bind` \c ->
|
satisfy p = item `bind` \c ->
|
||||||
if p c
|
if p c
|
||||||
then unit c
|
then unit c
|
||||||
else (Parser (\cs -> []))
|
else failure
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
-- Combinators
|
-- Combinators
|
||||||
|
Loading…
Reference in New Issue
Block a user