- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- C++ Program to convert the string into an integer
- Haskell Program to convert boolean variables into string
- Haskell Program to convert the string into a floating-point number
- Java Program to convert an integer into binary
- Haskell Program to Convert Boolean to String
- Golang Program to convert an integer into binary representation
- Haskell Program to convert long type variables into int
- Haskell Program to read an integer number from the user
- How to convert a string vector into an integer vector in R?
- C# Program to Convert Integer to String
- How do I convert a string into an integer in JavaScript?
- How to convert a string into integer in JavaScript?
- Haskell Program to convert a number into a complex number
- Java Program to convert from integer to String
- Java Program to convert from String to integer
