Haskell Program to convert the string into an integer


This tutorial will help us in converting the string into an integer. In Haskell, the process of converting a string to an integer typically involves using the read function or readMaybe function from the Text.Read module. Another approach is to use the digitToInt function from the Data.Char module to convert each character in the string to its corresponding numerical value.

All these functions are part of the standard library and we can use them to easily convert strings to integers in Haskell.

Algorithm

  • Step 1 − in-build() function is defined using read function.

  • Step 2 − Program execution will be started from main function. The main() function has whole control of the program.

  • Step 3 − A variable named, “s” will initialize. It will have the string value that is to be converted into integer.

  • Step 4 − in-build() function is being called and string will be passed as parameter to it.

  • Step 5 − Final resultant converted integer value is displayed.

Using Read Function

In this approach, stringToInt is defined under read function. This stringToInt() is then called and string value is passed as parameter to it. The show function is used to convert the integer to a string, so that it can be printed to the console.

Example

In this example, we are going to see that how we can convert the string into an integer using read function. The read function converts a string to a number

stringToInt :: String -> Int
stringToInt s = read s 
main :: IO ()
main = do 
   let s = "123"
   let result = stringToInt s
   putStrLn (show result)

Output

123

 Using readMaybe Function

In this example, we are going to see that how we can convert the string into an integer using readMaybe function from the Text.Read module. It will return the parsed value if the string can be parsed as the desired type and Nothing otherwise.The show function is used to convert the integer to a string, so that it can be printed to the console.

Example

import Text.Read (readMaybe)
stringToInt :: String -> Maybe Int
stringToInt s = readMaybe s
main :: IO ()
main = do 
   let s = "123"
   let result = stringToInt s
   putStrLn (show result)

Output

Just 123

Using digitToInt Function

In this method, the function stringToInt takes a string as an argument and uses pattern matching to check if it matches one of the predefined values ("one", "two", "three"). If it matches one of these values, it returns the corresponding integer wrapped in a Just constructor. The result is the integer representation of the original string. If the input string does not match any of the predefined values, it returns Nothing.

Example

import Data.Maybe (fromMaybe)
stringToInt :: String -> Maybe Int
stringToInt s = case s of
  "one"   -> Just 1
  "two"   -> Just 2
  "three" -> Just 3
  _       -> Nothing
main = do
  let input = "two"
  let output = fromMaybe (error "Invalid input") (stringToInt input)
  print output

Output

2

Conclusion

In Haskell, the process of converting a string to an integer typically involves using the read function or readMaybe function from the Text.Read module.

Another approach is to use the digitToInt function from the Data.Char module to convert each character in the string to its corresponding numerical value, and then combine them with a list of powers of 10 and add them up to get the integer representation of the original string.

Updated on: 20-Jan-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements