Haskell program to read numbers from standard input


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.

This tutorial discusses reading numbers from standard input in the Haskell Programming language.

Haskell is a purely functional Language. Pure functions are the ones 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.

Examples of an IO functions are print,putStr,getLine,getChar etc

Algorithm steps

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

  • Read the input as a number using the read function.

  • Load the input value into a variable.

  • Printing the user input.

Reading Numbers From Standard Input Using The Getline Method

Example

Program to read numbers from standard input using the getLine method

main :: IO() main = do print("Please enter a number") -- taking user input using getLine function line <- getLine -- typecasting string to integer let number = (read line:: Int) -- printing the number print ("The input number is:") print number

Input

"Please enter a number"
25

Output

“The inputted number is:”
123

In the above program we invoked the getLine function which is IO function to take user input. We loaded the input value into a variable line. We type casted the variable line into an Integer using the read function. Finally, we printed the inputted number.

Note− syntax “<-” must be used to load the value of an IO function into a variable.

Reading List Of Numbers From User Input

Example

Program to read list of numbers

-- function declaration readNums :: [Int]->IO () -- function definition readNums xs = do putStrLn("Please enter another number in the list") input<-getLine let number = (read input :: Int) if (number == -1) then do print ("The Input List of numbers are:") print (init xs) else do readNums (number:xs) main :: IO() main = do -- initializing list with initial value let list = [-1] -- invoking readNums function readNums list

Input

Please enter another number in the list list
1
Please enter another number in the list list
2
Please enter another number in the list list
33
Please enter another number in the list list
-1

Output

"The Input List of numbers are:"
[33,2,1]

In the above program we declared a function readNums as it takes a list of integers as input and returns an IO action. In the function definition, it takes an argument xs which is a list of integers. Next, take input from the user using the getLine function and loaded the inputted value into a variable input. We are typecasting the variable input into an integer and loading the value into a variable number. We are checking whether the value of a number is equal to ‘-1’. If the value of a number is equal to ‘-1’ we are printing the initial section of list xs. If the number is not equal to ‘-1’, we are recursively calling the readNums function with the argument number concatenated with the list xs. I.e the function loads the inputted values into a list until the inputted value is equal to ‘-1’. We invoked this function in the main.

Conclusion

In this tutorial, we discussed How to read numbers from standard input in Haskell programming language.

Updated on: 27-Oct-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements