`readMay`和`readMaybe`有什么区别?

时间:2018-02-07 10:54:31

标签: haskell

两个函数readMayreadMaybe具有相同的签名Read a => String -> Maybe a

它们之间有什么区别吗?如果是这样,他们是什么?哪两种功能应该首选?

1 个答案:

答案 0 :(得分:6)

没有区别。以下是gulp.task('cssInject', ['styles'], function() { browserSync.stream(); }); 定义的方式:

readMay

这是-- | This function provides a more precise error message than 'readEither' from 'base'. readEitherSafe :: Read a => String -> Either String a readEitherSafe s = case [x | (x,t) <- reads s, ("","") <- lex t] of [x] -> Right x [] -> Left $ "no parse on " ++ prefix _ -> Left $ "ambiguous parse on " ++ prefix where maxLength = 15 prefix = '\"' : a ++ if length s <= maxLength then b ++ "\"" else "...\"" where (a,b) = splitAt (maxLength - 3) s readMay :: Read a => String -> Maybe a readMay = eitherToMaybe . readEitherSafe

readMaybe

它们在中间错误消息中有所不同(-- | Parse a string using the 'Read' instance. -- Succeeds if there is exactly one valid result. -- A 'Left' value indicates a parse error. -- -- @since 4.6.0.0 readEither :: Read a => String -> Either String a readEither s = case [ x | (x,"") <- readPrec_to_S read' minPrec s ] of [x] -> Right x [] -> Left "Prelude.read: no parse" _ -> Left "Prelude.read: ambiguous parse" where read' = do x <- readPrec lift P.skipSpaces return x -- | Parse a string using the 'Read' instance. -- Succeeds if there is exactly one valid result. -- -- @since 4.6.0.0 readMaybe :: Read a => String -> Maybe a readMaybe s = case readEither s of Left _ -> Nothing Right a -> Just a 显示输入),但结果将相同。

readEitherSafe的{​​p> readMay早于Safe readMaybe。除非您的Text.Read版本低于4.6.0.0,否则请使用base中的readMaybe,因为它不需要其他软件包。