문제

There are some stdlib functions that throw errors on invalid input. For example:

Prelude> read "1o2" :: Int
*** Exception: Prelude.read: no parse

I would like to wrap it to return a Either e a instead. How can I do that?

도움이 되었습니까?

해결책

There is no spoon. You didn't hear it from me.

For this particular example, though, you should use reads instead.

다른 팁

I prefer to turn errors into values:

 maybeRead :: Read a => String -> Maybe a
 maybeRead s = case reads s of
      [(x, "")] -> Just x
      _         -> Nothing
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top