Applicative Parsing

使用 Applicative 式的 Parser。

包括使用 (<$>), (<*>), (<$), (<*), (*>), (<|>), many 等运算符。

应用实例1

import Control.Monad
import Text.Parsec
import Control.Applicative hiding ((<|>)) number = many1 digit
plus = char '+' *> number
minus = (:) <$> char '-' <*> number
integer = plus <|> minus <|> number
float = fmap rd $ (++) <$> integer <*> decimal
where rd = read :: String -> Float
decimal = option "" $ (:) <$> char '.' <*> number main = forever $ do putStrLn "Enter a float: "
input <- getLine
parseTest float input
*Main> main
Enter a float:
2.3
2.3
Enter a float:
1
1.0
Enter a float:
-1
-1.0
Enter a float:
+6.98
6.98