【发布时间】:2012-06-07 06:41:30
【问题描述】:
有些 stdlib 函数会在输入无效时引发错误。例如:
Prelude> read "1o2" :: Int
*** Exception: Prelude.read: no parse
我想将其包装成返回Either e a。我该怎么做?
【问题讨论】:
标签: haskell exception-handling either
有些 stdlib 函数会在输入无效时引发错误。例如:
Prelude> read "1o2" :: Int
*** Exception: Prelude.read: no parse
我想将其包装成返回Either e a。我该怎么做?
【问题讨论】:
标签: haskell exception-handling either
There is no spoon.你没听我说。
不过,对于这个特定示例,您应该改用 reads。
【讨论】:
我更喜欢将错误转化为价值:
maybeRead :: Read a => String -> Maybe a
maybeRead s = case reads s of
[(x, "")] -> Just x
_ -> Nothing
【讨论】:
read 仅用作示例。