Haskell Program to print integer entered by the user

Program

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

This Haskell program takes an integer input from the user and prints it to the console.

  1. prompt Function

    • Takes a string s (prompt message).
    • Prints s using putStrLn.
    • Reads user input using getLine and returns it as a string.
  2. main Function

    • Calls prompt with the message "Enter an integer number".
    • Stores the user input in number.
    • Prints "Entered number: ".
    • Converts number to an integer using read and prints it using print.

Key Takeaways

  • getLine reads input as a string.
  • read number :: Int converts the input string to an integer.
  • print is used to output the integer.

Output

$ ghc read-int.hs 
[1 of 1] Compiling Main             ( read-int.hs, read-int.o )
Linking read-int ...
$ ./read-int 
Enter a integer number
6
Entered number: 
6