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

<div class="execute"></div><div class="code-mirror  language-haskell" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token hvariable">main</span> <span class="token operator">::</span> <span class="token constant">IO</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token hvariable">main</span> <span class="token operator">=</span> <span class="token keyword">do</span>
   <span class="token builtin">print</span><span class="token punctuation">(</span><span class="token string">"Please enter the input value/string here"</span><span class="token punctuation">)</span>
<span class="token comment">-- initializing variable with output from getLine function</span>
   <span class="token hvariable">line</span> <span class="token operator"><-</span> <span class="token builtin">getLine</span>
   <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token string">"The user input is:"</span><span class="token punctuation">)</span>
   <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token hvariable">line</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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

<div class="execute"></div><div class="code-mirror  language-haskell" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token hvariable">main</span> <span class="token operator">::</span> <span class="token constant">IO</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token hvariable">main</span> <span class="token operator">=</span> <span class="token keyword">do</span>
<span class="token comment">-- initializing variable with output from getChar function</span>
   <span class="token hvariable">ch</span> <span class="token operator"><-</span> <span class="token builtin">getChar</span>
   <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token string">"The user input is:"</span><span class="token punctuation">)</span>
   <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token hvariable">ch</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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

<div class="execute"></div><div class="code-mirror  language-haskell" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token hvariable">inputStrings</span> <span class="token operator">::</span> <span class="token punctuation">[</span><span class="token constant">String</span><span class="token punctuation">]</span> <span class="token operator">-></span> <span class="token constant">IO</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token hvariable">inputStrings</span> <span class="token hvariable">xs</span> <span class="token operator">=</span> <span class="token keyword">do</span>
<span class="token comment">-- getting user input</span>
   <span class="token builtin">print</span><span class="token punctuation">(</span><span class="token string">"Enter another string or type exit to get the list"</span><span class="token punctuation">)</span>
   <span class="token hvariable">input</span> <span class="token operator"><-</span> <span class="token builtin">getLine</span>
   <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token hvariable">input</span> <span class="token operator">==</span> <span class="token string">"exit"</span><span class="token punctuation">)</span>
   <span class="token keyword">then</span> <span class="token keyword">do</span>
   <span class="token builtin">print</span> <span class="token string">"The user inputted list:"</span>
   <span class="token punctuation">(</span><span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token builtin">init</span> <span class="token hvariable">xs</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
   <span class="token keyword">else</span> <span class="token punctuation">(</span><span class="token hvariable">inputStrings</span> <span class="token punctuation">(</span><span class="token hvariable">input</span><span class="token operator">:</span><span class="token hvariable">xs</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
   
<span class="token hvariable">main</span> <span class="token operator">::</span> <span class="token constant">IO</span> <span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token hvariable">main</span> <span class="token operator">=</span> <span class="token keyword">do</span>
<span class="token comment">-- initializing list with an initial value</span>
   <span class="token keyword">let</span> <span class="token hvariable">list</span><span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">"initial"</span><span class="token punctuation">]</span>
<span class="token comment">-- invoking inputStrings functions</span>
   <span class="token hvariable">inputStrings</span> <span class="token hvariable">list</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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: 2022-10-27T06:02:32+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements