1

The read function is useful for converting String to Int, but it does not support error handling. Especially, it silently returns an incorrect value when the string value is too large to be represented as Int:

> read "123456789012345678901234567890" :: Int
-4362896299872285998

How can I check if the string value is between minBound :: Int and maxBound :: Int and thus correctly converted to Int? Is there an overflow-detecting conversion function that returns Maybe Int or something?

0

1 Answer 1

5

You can convert it first to Integer, and perform the comparison:

> (read "123456789012345678901234567890" :: Integer) > toInteger(maxBound::Int)
True
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.