如何使用parsec解析Haskell中的Tuple(String,Int) [英] How to parse a Tuple (String,Int) in Haskell using parsec

查看:96
本文介绍了如何使用parsec解析Haskell中的Tuple(String,Int)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我已经设法将字符串和字符串解析为Ints,但是我还需要解析(String,Int)类型,因为userRatings是为了正确读取textFile并且我正在使用Parsec



这是解析以及导入

  import Text.Parsec 
(Parsec,ParseError,parse - 类型和解析器
,between,noneOf,sepBy,many1 - 组合器
,char,空格,数字,换行符 - 简单解析器


- 将字符串解析为字符串
stringLit :: Parsec字符串u字符串
stringLit =之间(char''')(char''')$ many1 $ noneOf\\ \\n

- 将字符串解析为字符串列表
listOfStrings :: Parsec字符串u [String]
listOfStrings = stringLit`sepBy`(char' ,>>空格)

- 将字符串解析为一个int
intLit :: Parsec字符串u Int
intLit = fmap读取$ many1数字
- 或`用Control.Applic读取< $> many1 digit` ative

film :: Parsec String u电影
电影=做
- 或者`title< - stringLit< * newline` with Control.Applicative
title < - stringLit
newline
director< - stringLit
newline
year< - intLit
newline
userRatings< - listOfStrings
newline
return(title,director,year,userRatings)


解决方案

您可以通过使用 Applicative Monad 接口从现有解析器进行组合来完成此操作。 解析器具有两者的实例。

使用应用 $ b $>

  stringIntTuple :: Parser(String,Int)
stringIntTuple =(,)< $> yourStringParser< *> yourIntParser

这与以下内容相同:

  stringIntTuple :: Parser(String,Int)
stringIntTuple = liftA2(,)yourStringParser yourIntParser

使用 Monad

  stringIntTuple :: Parser(String,Int)
stringIntTuple =
do
theString< - yourStringParser
theInt< - yourIntParser
return(theString,theInt)


I think i already managed to parse strings to strings and strings to Ints but I also need to parse a (String,Int) type as userRatings is in order to read from a textFile correctly and I am using Parsec

This is the Parsing along with the import

import Text.Parsec
( Parsec, ParseError, parse        -- Types and parser
, between, noneOf, sepBy, many1    -- Combinators
, char, spaces, digit, newline     -- Simple parsers
)

-- Parse a string to a string
stringLit :: Parsec String u String
stringLit = between (char '"') (char '"') $ many1 $ noneOf "\"\n"

-- Parse a string to a list of strings
listOfStrings :: Parsec String u [String]
listOfStrings = stringLit `sepBy` (char ',' >> spaces)

-- Parse a string to an int
intLit :: Parsec String u Int
intLit = fmap read $ many1 digit
-- Or `read <$> many1 digit` with Control.Applicative

film :: Parsec String u Film
film = do
-- alternatively `title <- stringLit <* newline` with Control.Applicative
title <- stringLit
newline
director <- stringLit
newline
year <- intLit
newline
userRatings <- listOfStrings
newline
return (title, director, year, userRatings)

解决方案

You can do this by composing from existing parsers using either the Applicative or the Monad interface. Parser has instances of both.

Using Applicative:

stringIntTuple :: Parser (String, Int)
stringIntTuple = (,) <$> yourStringParser <*> yourIntParser

Which is the same as the following:

stringIntTuple :: Parser (String, Int)
stringIntTuple = liftA2 (,) yourStringParser yourIntParser

Using Monad:

stringIntTuple :: Parser (String, Int)
stringIntTuple =
  do
    theString <- yourStringParser
    theInt <- yourIntParser
    return (theString, theInt)

这篇关于如何使用parsec解析Haskell中的Tuple(String,Int)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆