Haskell Language Learning Note (parsec) (3)

Source: Internet
Author: User

Applicative parsing

Use the applicative type of Parser.
Operators include use (<$>), (<*>), (<$), (<*), (*>), (<|>), many, and so on.

import Control.Monad-- showimport Text.Parsecimport Control.Applicative hiding ((<|>))number = many1 digitplus = char '+' *> numberminus = (:) <$> char '-' <*> numberinteger = plus <|> minus <|> numberfloat = fmap rd $ (++) <$> integer <*> decimal    where rd      = read :: String -> Float          decimal = option "" $ (:) <$> char '.' <*> number-- /showmain = forever $ do putStrLn "Enter a float: "                    input <- getLine                    parseTest float input
*Main> mainEnter a float: 2.32.3Enter a float: 11.0Enter a float: -1-1.0Enter a float: +6.986.98
    • Plus = char ' + ' *> number
      P1 *> P2 matches p1 and P2 in turn, but P1 is discarded, returning only P2.
    • minus = (:) <$> char '-' <*> number
      (:) <$> p1 <*> p2 match p1 and P2 in turn, and then bind the results of two matches.
      Here P1 matches the result is the character, P2 matches the result is the string, therefore uses (:) To connect.
    • float = Fmap Rd $ (+ +) <$> integer <*> decimal
      (+ +) <$> p1 <*> p2 match p1 and P2 in turn, and then bind the results two times.
      The results of the two matches here are all strings, so use (+ +) to connect.
    • decimal = Option "" $ (:) <$> char '. ' <*> number
      Option "" P tries to match p if unsuccessful, returns an empty string "".
Reference links

Parsing floats with parsec

Haskell Language Learning Note (parsec) (3)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.