- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Haskell program to print an integer
This tutorial will discuss writing a program to print an Integer in Haskell Programming Language. The computations in Haskell programming language are done through Mathematical functions.
In this tutorial, We will discuss different ways to print an Integer in Haskell Language.
Printing an integer declared outside of functions (global scope).
Printing an integer that is declared inside functions using the IO system.
Printing an integer concatenated with Strings.
Integers are numbers that are not fractions. Integers are also called whole numbers. Integers can be negative, positive, or zero. Example 12, 1, and -100 are Integer because they don’t have any fractions. Example 1.23, 0.12, and ½ are not integers because they are fractions.
Following is the syntax for printing an Integer in Haskell programming Language.
We start with declaring a variable for storing an integer and loading an Integer into it.
The syntax for declaring and defining Integer in functions using IO operations
Example: functions using Input Output (I/O) system.
In this function, variables can be declared and defined instantaneously using the “let” keyword.
The above line loads an Integer value into num. We can load other data type values in the variable. Once initialized the data type strictly binds to the variable. The type of the variable is decided when it is initialized.
The syntax for declaring and defining integers outside functions and functions not using IO operations
num :: Int
The above line declares the variable num as Int.
Note − The above line is optional if data type declaration is not done data type binding occurs when it is initialized.
num = 10
The above line initializes the variable num with an integer value of 10. We cannot load the other data types as we declared the variable as an Integer. The compiler throws an error another data type is loaded. Here type of variable is decided at compile time.
declaring a variable (optional).
initializing the variable.
Program to print Integer that is declared outside functions.
-- declaring variable num as Int num :: Int -- initializing variable num with value 10 num = 10 main :: IO () main = do -- printing variable num print "The value of the Integer num is:" print num
The value of the Integer num is: 10
Note − lines starting with “--” are single comments which are ignored by the Haskell compiler during execution
In the above program as we declared the integer outside the function and initialized it with the value 10 and printed the variable “num” in the main function.
Program to print Integer that is declared inside functions using IO system.
main :: IO () main = do -- the variable is declared and initialized withthe let keyword let num = 10 -- printing variable num print "The value of the Integer num is:" print num
The value of the Integer num is: 10
In the above function the variable “num” is declared using the let keyword and initialized with value 10 and printed the value of variable num in the main function.
Program to print an Integer concatenated with a String
main :: IO () main = do -- the variable is declared and initialized let num = 10 -- printing variable num concatenating with String print ("The value of the Integer num is:" ++ show num)
The value of the Integer num is:10
Note − Show is a function that takes any primitive or non-primitive data type as an argument and returns the value as a String.
So in this article, we have understood how to print integers in ‘Haskell Programming Language’ in different scenarios Like without concatenating and with concatenating Strings.
- Java Program to Print an Integer
- Java program to accept an integer from user and print it
- C++ Program to print unique integer pairs
- 8086 program to print the table of input integer
- 8085 program to print the table of input integer
- How to Read and Print an Integer value in C++
- Java Program to Print a Square Pattern for given integer
- Java Program to convert an integer into binary
- C++ program to Zoom digits of an integer
- Python Program to Print an Identity Matrix
- Golang Program to Print an Identity Matrix
- Python program to print all distinct elements of a given integer array.
- Incrementing an integer inside loop in an ABAP program
- Python Program to Count set bits in an integer
- Java Program to Count set bits in an integer
- Golang Program to convert an integer into binary representation