本文介绍了在解析字符串的地方实现Read typeclass包括“$”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我一直在玩Haskell大约一个月。对于我的第一个真正的Haskell项目,我正在编写一个词性标注器。作为这个项目的一部分,我有一个名为 Tag 的类型,它表示一个词性标记,实现如下: 数据Tag = CC | CD | DT | EX | FW | IN | JJ | JJR | JJS ... 以上是一系列标准化的词性标签, ve被故意截断。然而,在这个标准的标签集中,有两个以美元符号($)结尾:PRP $和NNP $。因为我不能在名称中使用$类型的构造函数,所以我已经选择将它们重命名为PRPS和NNPS。 这很好,但我' d喜欢从词典中的字符串中读取标签,并将它们转换为我的 Tag 类型。尝试此操作失败: 实例读取标记其中 readsPrec _ input = (\inp - > ; [((NNPS),rest)|(NNP $,rest) $中的Haskell词法分析器扼流器。任何想法如何将其取消? 实现Show非常简单。如果有一些类似的Read策略,那将是非常棒的。 instance显示标签其中显示了Prec _ NNPS = showString NNP $ showsPrec_PRPS = showStringPRP $ showsPrec_tag =显示标签 解决方案您在这里滥用 Read > 显示和 Read 用于打印和解析有效的Haskell值,以启用调试等。总是完美的(例如,如果您导入 Data.Map 限定,然后在 Map上调用 show 值,调用 fromList 不合格),但这是一个有效的起点。 如果你想打印或解析你的值以匹配某种特定的格式,那么为前者使用一个漂亮的打印库,后者使用一个真正的解析库(例如uu-parsinglib,polyparse,parsec等)。它们通常比 ReadS (尽管GHC中的 ReadP 不是 bad)。 虽然你可能会认为这不是必要的,但这只是你正在做的一个快速的'黑客',快'肮脏的黑客往往会徘徊......做自己的忙,并在第一时间做对:这意味着当你想稍后正确地做时,重写的次数会减少。 I've been playing with Haskell for about a month. For my first "real" Haskell project I'm writing a parts-of-speech tagger. As part of this project I have a type called Tag that represents a parts-of-speech tag, implemented as follows:data Tag = CC | CD | DT | EX | FW | IN | JJ | JJR | JJS ...The above is a long list of standardized parts-of-speech tags which I've intentionally truncated. However, in this standard set of tags there are two that end in a dollar sign ($): PRP$ and NNP$. Because I can't have type constructors with $ in their name, I've elected to rename them PRPS and NNPS.This is all well and good, but I'd like to read tags from strings in a lexicon and convert them to my Tag type. Trying this fails:instance Read Tag where readsPrec _ input = (\inp -> [((NNPS), rest) | ("NNP$", rest) <- lex inp]) inputThe Haskell lexer chokes on the $. Any ideas how to pull this off?Implementing Show was fairly straightforward. It would be great if there were some similar strategy for Read.instance Show Tag where showsPrec _ NNPS = showString "NNP$" showsPrec _ PRPS = showString "PRP$" showsPrec _ tag = shows tag 解决方案 You're abusing Read here.Show and Read are meant to print and parse valid Haskell values, to enable debugging, etc. This doesn't always perfectly (e.g. if you import Data.Map qualified and then call show on a Map value, the call to fromList isn't qualified) but it's a valid starting point.If you want to print or parse your values to match some specific format, then use a pretty-printing library for the former and an actual parsing library (e.g. uu-parsinglib, polyparse, parsec, etc.) for the latter. They typically have much nicer support for parsing than ReadS (though ReadP in GHC isn't too bad).Whilst you may argue that this isn't necessary, this is just a quick'n'dirty hack you're doing, quick'n'dirty hacks have a tendency to linger around... do yourself a favour and do it right the first time: it means there's less to re-write when you want to do it "properly" later on. 这篇关于在解析字符串的地方实现Read typeclass包括“$”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-01 22:51