mirror of
https://github.com/chrisdone/duet.git
synced 2025-01-08 06:53:22 +03:00
Add parser example
This commit is contained in:
parent
19e0e51d9d
commit
3d0ec5488a
16
examples/parser.hs
Normal file
16
examples/parser.hs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
data Result a = OK a String | Error String
|
||||||
|
data Parser a = Parser (String -> Result a)
|
||||||
|
parseBool =
|
||||||
|
Parser
|
||||||
|
(\string ->
|
||||||
|
case take 4 string of
|
||||||
|
"True" -> OK True (drop 4 string)
|
||||||
|
_ ->
|
||||||
|
case take 5 string of
|
||||||
|
"False" -> OK False (drop 5 string)
|
||||||
|
_ -> Error (append "Expected a bool, but got: " string))
|
||||||
|
runParser =
|
||||||
|
\p s ->
|
||||||
|
case p of
|
||||||
|
Parser f -> f s
|
||||||
|
main = runParser parseBool "TrueFalse"
|
Loading…
Reference in New Issue
Block a user