# Haskell program to print an integer

#### Complete Python Prime Pack

9 Courses     2 eBooks

#### Artificial Intelligence & Machine Learning Prime Pack

6 Courses     1 eBooks

#### Java Prime Pack

9 Courses     2 eBooks

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.

## Prerequisite

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.

## Syntax

Following is the syntax for printing an Integer in Haskell programming Language.

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.

let num=100


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.

## Algorithm steps

• declaring a variable (optional).

• initializing the variable.

• printing Integer.

## Example 1

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


## Output

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.

## Example 2

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


## Output

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.

## Example 3

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)


## Output

The value of the Integer num is:10


## Conclusion

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.

Updated on 11-Oct-2022 11:35:30