-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ch24_Backtracking.hs
48 lines (39 loc) · 982 Bytes
/
Ch24_Backtracking.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{-# LANGUAGE OverloadedStrings #-}
module BT where
import Control.Applicative
import qualified Data.Attoparsec.ByteString as A
import Data.Attoparsec.ByteString (parseOnly)
import Data.ByteString (ByteString)
import Text.Trifecta hiding (parseTest)
import Text.Parsec (Parsec, parseTest)
trifP
:: Show a
=> Parser a -> String -> IO ()
trifP p i = print $ parseString p mempty i
parsecP
:: Show a
=> Parsec String () a -> String -> IO ()
parsecP = parseTest
attoP
:: Show a
=> A.Parser a -> ByteString -> IO ()
attoP p i = print $ parseOnly p i
nobackParse
:: (Monad f, CharParsing f)
=> f Char
nobackParse = (char '1' >> char '2') <|> char '3'
tryParse
:: (Monad f, CharParsing f)
=> f Char
tryParse = try (char '1' >> char '2') <|> char '3'
main :: IO ()
main = do
-- trifecta
trifP nobackParse "13"
trifP tryParse "13"
-- parsec
parsecP nobackParse "13"
parsecP tryParse "13"
-- attoparsec
attoP nobackParse "13"
attoP tryParse "13"