Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell “readMay”和“readMay”有什么区别?_Haskell - Fatal编程技术网

Haskell “readMay”和“readMay”有什么区别?

Haskell “readMay”和“readMay”有什么区别?,haskell,Haskell,两个函数readMay和readMaybe具有相同的签名reada=>String->Maybe a 他们之间有什么区别吗?如果是,它们是什么?这两个功能中哪一个应该是首选的?没有区别。以下是readMay的定义: -- | This function provides a more precise error message than 'readEither' from 'base'. readEitherSafe :: Read a => String -> Either Str

两个函数
readMay
readMaybe
具有相同的签名
reada=>String->Maybe a


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

没有区别。以下是
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
它们在中间错误消息中有所不同(
readEitherSafe
显示输入),但结果相同



readMay
from
Safe
早于
readMay
from
Text.Read
。除非您的
基础
版本低于4.6.0.0,否则请使用
文本中的
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