Read Integer Number from User in Haskell



This tutorial will help us to read an integer number from the user. The user is prompted to enter any integer number. Then the entered integer is displayed to the console.

Method 1: Using read and getLine function

This approach uses the read and getLine function to attempt to parse the input as an integer and display the integer number to the console.

Algorithm

  • Step 1 ? main :: IO () is defining the main function.

  • Step 2 ? putStrLn "Enter an integer: " prints the message asking for the user input.

  • Step 3 ? input <- getLine this is where the user input is stored in the variable input.

  • Step 4 ? let number = read input :: Int reads the input as integer and stores it in the variable number.

  • Step 5 ? putStrLn ("You entered: " ++ show number) finally prints the input entered by the user.

Example

Program to read an integer number from the user by using read and getLine function.

main :: IO () main = do putStrLn "Enter an integer: " input <- getLine let number = read input :: Int putStrLn ("You entered: " ++ show number)

Output

Enter an integer: 
-20
You entered: -20

Method 2: Using readLn function

This approach uses the readLn function to attempt to parse the input as an integer and display the integer number to the console.

Algorithm

  • Step 1 ? main :: IO () is defining the main function.

  • Step 2 ? putStrLn "Enter an integer: " prints the message asking for the user input.

  • Step 3 ? number <- readLn :: IO Int this is where the user input is read as an integer and stored in the variable number.

  • Step 4 ? putStrLn ("You entered: " ++ show number) finally prints the input entered by the user.

Example

Program to read an integer number from the user by using readLn function.

main :: IO () main = do putStrLn "Enter an integer: " number <- readLn :: IO Int putStrLn ("You entered: " ++ show number)

Output

Enter an integer: 
-3
You entered: -3

Method 3: Using readMaybe function

This approach uses the readMaybe function to attempt to parse the input as an integer and returns Just number if successful or Nothing if not.

Algorithm

  • Step 1 ? import Text.Read (readMaybe) imports the readMaybe function from the Text.Read module.

  • Step 2 ? main :: IO () defines the main function with the IO monad.

  • Step 3 ? putStrLn "Enter an integer: " prints the message asking for the user input.

  • Step 4 ? input <- getLine reads the input from the user and stores it in the input variable.

  • Step 5 ? readMaybe input :: Maybe Int uses the readMaybe function to attempt to parse the input as an integer, and returns Just number if successful, or Nothing if not. The :: Maybe Int specifies the type of the input as Maybe Int.

  • Step 6 ? Just number -> putStrLn ("You entered: " ++ show number) if the input is a valid integer, it prints the input entered by the user.

  • Step 7 ? Nothing -> putStrLn "Invalid input. Please enter a valid integer." if the input is not a valid integer it prints an error message to the user, prompting them to enter a valid integer.

Example

Program to read an integer number from the user by using readMaybe function.

import Text.Read (readMaybe) main :: IO () main = do putStrLn "Enter an integer: " input <- getLine case readMaybe input :: Maybe Int of Just number -> putStrLn ("You entered: " ++ show number) Nothing -> putStrLn "Invalid input. Please enter a valid integer."

Output

Enter an integer: 
-7
You entered: -7

Method 4: Using prompt function

This approach uses the prompt function to attempt to parse the input as an integer and returns the number to the console.

Algorithm

  • Step 1 ? prompt s = do and putStrLn s is used to prompt for the input from user.

  • Step 2 ? number <- getLine reads the input from the user and stores it in the input variable.

  • Step 3 ? return number, returns number to console.

  • Step 4 ? main = do calls the main function.

  • Step 5 ? number <- prompt "Enter a integer number" prints the message asking for the user input by calling prompt function.

  • Step 6 ? putStrLn ("Entered number: ") and print $ (read number :: Int) is used to print the input entered by the user.

Example

Program to read an integer number from the user by using prompt function.

prompt s = do putStrLn s number <- getLine return number main = do number <- prompt "Enter a integer number" putStrLn ("Entered number: ") print $ (read number :: Int)

Output

Enter a integer number
-6
Entered number: 
-6

Conclusion

In Haskell, there are various ways to read an integer number from the user. This can be achieved by using read function, getLine function, readLn function, readMaybe function or prompt function. In every approach, the user is prompted to enter any integer number. And then the entered integer is displayed to the console as output.

Updated on: 2023-01-23T11:49:39+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements