Haskell Program To Add Two Numbers

HaskellServer Side ProgrammingProgramming

This tutorial will discuss writing a program to add two numbers in Haskell Programming Language. Haskell is a functional, declarative, and strongly typed language. It supports lazy evaluation (evaluated only when required). The computations in Haskell are Mathematical functions.

This tutorial discusses different ways to add two numbers in Haskell.

  • Implementing addition computation using infix notation.

  • Implementing addition computation using prefix notation.

  • Implementing addition computation with a separate reusable function.

  • Implementing addition computation using recursive function.

  • Implementing addition computation using operator sections.

Syntax

Following is the syntax structure to add two numbers

We start with taking the first number followed by the addition operator (+) and finally followed by the second number.

number_1 + number_2

where “+” is an infix function used for addition computation

Algorithm steps

  • Declare two variables for the number to be added. (declaring variable is optional in Haskell )

  • Initialize the variables with value

  • Add the variable with the appropriate infix function for addition (“+”).

Example 1

In this example we are going ot Implementing addition computation using infix notation.

main :: IO() main = do -- declaring first variable num1 let num1 = 10 -- declaring second variable num2 let num2 = 20 -- declaring variable result to store the computed value let result = num1 + num2 -- printing the result value print ("The Addition of two numbers 10 and 20 is:") print (result)

Output

The Addition of two numbers 10 and 20 is:
30

Note − lines starting with “--” are single line comments which are ignored by the compiler during execution.

In the above program we first declared and initialized two variables num1 and num1 with values 10 and 20. Then we computed the addition of two using the infix function “+” as said, Haskell is a functional programming language all computations are implementations of functions. Here addition operator ‘+’ is called an infix function because it is placed between the operands for computation. And finally, we loaded the computed result into a variable result and printed it on the output screen.

Example 2

Program to add two numbers with prefix notation.

main :: IO() main = do -- declaring first variable num1 let num1 = 30 -- declaring second variable num2 let num2 = 10 -- declaring variable result to store the computed value let result = (+) num1 num2 -- printing the result value print ("The Addition of two numbers 30 and 10 is:") print (result)

Output

The Addition of two numbers 30 and 10 is:
40

In the above program the Addition computation is done using an infix function “+” as a prefix, we can convert the infix function to a prefix function by encapsulating it with brackets “(+)”. And we can pass the operands as arguments to a function that returns the value of the computation and finally, we printed the result.

Example 3

Program to add two numbers using a function.

-- function to compute addition of two numbers -- function declaration addition :: Int->Int->Int -- function definition addition x y = x + y main :: IO() main = do -- declaring first variable num1 let num1 = 10 -- declaring second variable num2 let num2 = 20 -- invoking function to compute result and loading value into result variable let result = addition num1 num2 -- printing the result value print ("The Addition of two numbers 10 and 20 is:") print (result)

Output

"The Addition of two numbers 10 and 20 is:"
30

In the above program We declared the function addition which takes 2 integers as an input and returns an integer. Then we defined this function which takes 2 operands and returns the value of adding both numbers. And finally, We invoked the addition function in the main functions and loaded it in the result which is printed later.

Example 4

Program to add two numbers using recursive function.

-- function to compute addition of two numbers recursively -- function declaration addition :: Int->Int->Int -- function definition -- base case for function returns x when second operand is zero addition x 0 = x -- recursively add 1 to the value to be returned until y = 0 addition x y = 1 + addition x (y-1) main :: IO() main = do -- declaring first variable num1 let num1 = 10 -- declaring second variable num2 let num2 = 50 -- invoking function to compute result and loading value into result variable let result = addition num1 num2 -- printing the result value print ("The Addition of two numbers 10 and 50 is:") print (result)

Output

"The Addition of two numbers 10 and 20 is:"
60

In the above program we defined a recursive function addition which takes input two arguments and returns an integer. Haskell supports pattern matching for function arguments. So, we can define different implementations of a function for different patterns of arguments. Here we defined a base case for the function which returns the first argument if the second argument is zero otherwise it adds 1 to the value to be returned until the second argument is zero. The logic is we add the value ‘1’ y times and add x in the base case which equals the addition of two numbers. Finally, we invoked the recursive function, loaded the value into the variable result, and printed the result.

Example 5

Program to add two numbers using the operator sections.

main :: IO() main = do -- declaring first variable num1 let num1 = 10 -- declaring second variable num2 let num2 = 70 let result = (num1+) num2 -- printing the result value print ("The Addition of two numbers 10 and 70 is:") print (result)

Output:

"The Addition of two numbers 10 and 20 is:"
80

In this program the addition computation is done using an application of an infix function as a prefix. We called this a section operator. we wrap a value inside the operator enclosed with brackets (the value can be left or right of the operator). Now, this entity can be used as a function that takes a single argument and adds value to the argument like increment and decrement operators in some imperative programming languages. Example: “(-2)” subtracts value ‘2’ from the argument and returns the result.

Conclusion

In this tutorial we discussed different ways to add two numbers in Haskell Programming Language, using operators, functions, recursive functions, etc.

raja
Updated on 11-Oct-2022 11:13:42

Advertisements