読者です 読者をやめる 読者になる 読者になる

Convert String into Int in Haskell

There is well-known function read :: Read a => String -> a. Many blogs tell you to use "read" to convert string to number or something else (if it's a instance of Read class).

There are 3 problems.

  • Prelude Exception is told obsolete. use Control.Exception instead.
  • "read" produces Prelude.Exception.
  • In many case you are not sure you feed good stuff to "read".

In that case, use readMaybe :: Read a => String -> Maybe a. If you are not sure about Maybe, you should learn for your hereafter.