简体   繁体   English

为什么Haskell的Prelude.read没有返回Maybe?

[英]Why doesn't Haskell's Prelude.read return a Maybe?

Is there a good reason why the type of Prelude.read is Prelude.read的类型是否有充分的理由

read :: Read a => String -> a

rather than returning a Maybe value? 而不是返回一个Maybe值?

read :: Read a => String -> Maybe a

Since the string might fail to be parseable Haskell, wouldn't the latter be be more natural? 由于字符串可能无法解析Haskell,后者不会更自然吗?

Or even an Either String a , where Left would contain the original string if it didn't parse, and Right the result if it did? 或者甚至是Either String a ,其中Left将包含原始字符串(如果它不解析),如果它结果是Right的结果?

Edit: 编辑:

I'm not trying to get others to write a corresponding wrapper for me. 我不是想让其他人为我写一个相应的包装器。 Just seeking reassurance that it's safe to do so. 只是寻求保证这样做是安全的。

Edit : As of GHC 7.6, readMaybe is available in the Text.Read module in the base package, along with readEither : http://hackage.haskell.org/packages/archive/base/latest/doc/html/Text-Read.html#v:readMaybe 编辑 :从GHC 7.6开始, readMaybe可以在基础包的Text.Read模块中找到,还有readEitherhttpreadEither 的.html#五:readMaybe


Great question! 好问题! The type of read itself isn't changing anytime soon because that would break lots of things. 读取本身的类型不会很快改变,因为这会破坏很多东西。 However, there should be a maybeRead function. 但是, 应该有一个maybeRead函数。

Why isn't there? 为什么不存在? The answer is "inertia". 答案是“惯性”。 There was a discussion in '08 which got derailed by a discussion over "fail." 08年的讨论因“失败”的讨论而脱轨。

The good news is that folks were sufficiently convinced to start moving away from fail in the libraries. 好消息是,人们已经足够信服开始摆脱图书馆的失败。 The bad news is that the proposal got lost in the shuffle. 坏消息是该提议在洗牌中丢失了。 There should be such a function, although one is easy to write (and there are zillions of very similar versions floating around many codebases). 应该有这样一个函数,虽然一个很容易编写(并且有许多非常相似的版本在许多代码库中浮动)。

See also this discussion . 另见本讨论

Personally, I use the version from the safe package . 就个人而言,我使用的是安全包中的版本。

Yeah, it would be handy with a read function that returns Maybe. 是的,使用返回Maybe的read函数会很方便。 You can make one yourself: 你可以自己做一个:

readMaybe :: (Read a) => String -> Maybe a
readMaybe s = case reads s of
              [(x, "")] -> Just x
              _ -> Nothing

Apart from inertia and/or changing insights, another reason might be that it's aesthetically pleasing to have a function that can act as a kind of inverse of show . 除了惯性和/或改变见解之外,另一个原因可能是,在美学上令人愉悦的是具有可以作为一种show反转的功能。 That is, you want that read . show 也就是说,你想要那个read . show read . show is the identity (for types which are an instance of Show and Read ) and that show . read read . show是标识(对于ShowRead的实例的类型)和show . read show . read is the identity on the range of show (ie show . read . show == show ) show . readshow范围内的标识(即show . read . show == show

Having a Maybe in the type of read breaks the symmetry with show :: a -> String . read类型中使用Maybe会破坏show :: a -> String的对称性。

As @augustss pointed out, you can make your own safe read function. 正如@augustss指出的那样,您可以自己进行安全读取功能。 However, his readMaybe isn't completely consistent with read, as it doesn't ignore whitespace at the end of a string. 但是,他的readMaybe与read不完全一致,因为它不会忽略字符串末尾的空格。 (I made this mistake once, I don't quite remember the context) (我犯过这个错误,我不太记得上下文)

Looking at the definition of read in the Haskell 98 report , we can modify it to implement a readMaybe that is perfectly consistent with read , and this is not too inconvenient because all the functions it depends on are defined in the Prelude: Haskell 98报告中查看read定义 ,我们可以对其进行修改以实现与read完全一致的readMaybe ,这不是太不方便,因为它所依赖的所有函数都在Prelude中定义:

readMaybe        :: (Read a) => String -> Maybe a
readMaybe s      =  case [x | (x,t) <- reads s, ("","") <- lex t] of
                         [x] -> Just x
                         _   -> Nothing

This function (called readMaybe ) is now in the Haskell prelude! 这个函数(称为readMaybe )现在是Haskell的前奏! (As of the current base -- 4.6) (截至目前的基数--4.6)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM