Haskell program to get input from the user


In this tutorial, we discuss writing a program to get input from the user in the Haskell programming language. Haskell is a Declarative, Strongly Typed, and Functional programming language. Computations in Haskell are mathematical functions.

In this tutorial, we discuss two ways to get input from the user in Haskell.

  • User input using the getLine method.

  • User input using the getChar method.

As Haskell is a purely functional Language. Pure functions are the functions that return the same output for the same arguments. Taking user input changes the program’s nature to impure. Haskell introduced a type IO that differentiates impure functions from pure functions. A function declaration with type IO infers that it is an impure function that interacts with the outer world. “()” is an argument for an IO action that describes the returned value of an IO function.

Example

Below functions

fun1 :: IO (), returns empty tuple. (return statement is not necessary for empty tuple).
fun1 :: IO (Int), returns a tuple with a single integer. (return statement is necessary).

Syntax

As taking user input is an input/output operation. Functions with IO operation if declared must be declared with syntax IO ().

function_name :: IO ()

If there is more than one statement in an IO function the functions must use the do keyword to sequence the IO operations.

function_name = do
   statement1…
   statement2..

Note− Every IO function must have a return statement or an IO operation that returns IO actions.

Print/putStr/putChar must be used in an IO function without a return statement.

Algorithm steps

  • Take input from the user using the appropriate IO function.

  • Load the input value into a variable.

  • Printing the user input.

Getting User Input Using The Getline Method

Example

Program to get user input using the getLine method

main :: IO() main = do print("Please enter the input value/string here") -- initializing variable with output from getLine function line <- getLine print ("The user input is:") print (line)

Input

"Please enter the input value/string here"
Hi Let's play a game

Output

"The user input is:"
"Hi Let's play a game"

In the above program. We declared the main function with type IO () to use IO operations. We invoked the getLine function which takes strings user input. We loaded the value from the getLine function into a variable Line using the syntax “<-” to load returned variables from IO functions. Finally, we printed the variable line.

Note− We cannot print an IO function. An Example we cannot print the getLine function value from the function only accessible through the keyword “<-”

Getting First Letter Of User Input Using The Function Getchar

Example

Program to get first letter of user input using the function getChar

main :: IO() main = do -- initializing variable with output from getChar function ch <- getChar print ("The user input is:") print (ch)

Input

TutorialsPoint

Output

"The user input is:"
'T'

In the above program. We declared the main function with type IO () to use IO operations. We invoked the getChar function which takes Character as user input. We loaded the value from the getChar function into a variable ch using the syntax “<-” to load returned variables from IO functions. Finally, we printed the variable line.

Taking Multiple Strings As User Input And Prints Them As A List

Example

Program to take Multiple strings as user input and prints them as a list

inputStrings :: [String] -> IO() inputStrings xs = do -- getting user input print("Enter another string or type exit to get the list") input <- getLine if (input == "exit") then do print "The user inputted list:" (print (init xs)) else (inputStrings (input:xs)) main :: IO () main = do -- initializing list with an initial value let list=["initial"] -- invoking inputStrings functions inputStrings list

Input

"Enter another string or type exit to get the list"
Hi
"Enter another string or type exit to get the list"
Hello
"Enter another string or type exit to get the list"
How
"Enter another string or type exit to get the list"
exit

Output

"The user inputted list:"
["How","Hello","Hi"]

In the above program, we declared function inputStrings as takes list of Strings as user input and returns IO action. In the function definition, it takes a list xs as an argument. We used the do keyword to use IO operations. We invoked getLine which takes a string as user input. We loaded the value returned by the getLine function into a variable input. We are checking whether the input is equal to the string “exit” using the if keyword. If the input is equal to the string “exit”, we are printing the initial section of the list using the init function. Else we are recursively calling the function inputStrings with argument input concatenated with the list xs. I.e this function loads the strings into a list until the user input is not equal to “exit”.

Conclusion

In this tutorial, we discussed two ways to get user input in Haskell programming Language.

Updated on: 27-Oct-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements