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.
-
promptFunction- Takes a string
s(prompt message). - Prints
susingputStrLn. - Reads user input using
getLineand returns it as a string.
- Takes a string
-
mainFunction- Calls
promptwith the message"Enter an integer number". - Stores the user input in
number. - Prints
"Entered number: ". - Converts
numberto an integer usingreadand prints it usingprint.
- Calls
Key Takeaways
getLinereads input as a string.read number :: Intconverts the input string to an integer.printis 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