
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert String Type Variables into Int in Haskell
In Haskell, we will convert string type variables into integer by using user-defined function, stringToInt along with read and readMaybe functions. This can also be implemented using pattern matching and recursion. In the first example, we are going to use (stringToInt str = readMaybe str) function and in the second example, we are going to use (stringToInt "" = Nothing; stringToInt ('-':xs) = negate <$>; stringToInt xs; stringToInt xs = case reads xs of [(n, "")] -> Just n and _ -> Nothing).
Algorithm
Step 1 ? The stringToInt function is defined using readMaybe function
Step 2 ? The program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.
Step 3 ? The variable named, "inputStr" is being initialized. It will hold the string that is to be converted to respective integer value.
Step 4 ? The function, stringToInt is being called and inputStr is passed to it.
Step 5 ? The resultant Integer value is printed to the console using ?putStrLn' statement after the function is called.
Example 1
In this example, the stringToInt function is defined, which takes a String argument and returns a Maybe Int value. It then uses this function in the main function to read a number from the user and print it to the console. This can also be implemented using read function.
import Text.Read (readMaybe) stringToInt :: String -> Maybe Int stringToInt str = readMaybe str main :: IO () main = do let inputStr = "345" case stringToInt inputStr of Just n -> putStrLn $ "Integer value: " ++ show n Nothing -> putStrLn "Invalid input"
Output
Integer value: 345
Example 2
In this example, the user-defined function, stringToInt is defined to convert the string variables to int using read and isDigit function.
import Data.Char (isDigit) stringToInt :: String -> Maybe Int stringToInt str | all isDigit str = Just (read str) | otherwise = Nothing main :: IO () main = do let inputStr = "345" case stringToInt inputStr of Just n -> putStrLn $ "Integer value: " ++ show n Nothing -> putStrLn "Invalid input"
Output
Integer value: 345
Example 2
In this example, the pattern matching and recursion are used to handle different cases of the input string. The first pattern matches an empty string and returns Nothing. The second pattern matches a string that starts with a hyphen (-) and recursively applies stringToInt to the remaining characters, then applies the negate function to the resulting Maybe Int value. The third pattern uses the reads function to attempt to parse the input string as an Int and returns a Just value containing the parsed integer if the parsing is successful, or Nothing otherwise.
stringToInt :: String -> Maybe Int stringToInt "" = Nothing stringToInt ('-':xs) = negate <$> stringToInt xs stringToInt xs = case reads xs of [(n, "")] -> Just n _ -> Nothing main :: IO () main = do let inputStr = "345" case stringToInt inputStr of Just n -> putStrLn $ "Integer value: " ++ show n Nothing -> putStrLn "Invalid input"
Output
Integer value: 345
Conclusion
String to int conversion is the process of converting a string of characters that represent an integer value into an integer data type. In many programming languages, including Haskell, strings are used to represent textual data, while integers are used to represent numerical data. Converting a string to an integer allows the program to perform mathematical operations or comparisons with the numerical value represented by the string. In Haskell, a string variable is converted to int using user-defined stringToInt function along with read and readMaybe functions. And also by using recursion and pattern matching.