
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Sum of Natural Numbers Using Recursion in Haskell
In Haskell, we Find the Sum of Natural Numbers by using recursion and tail-recursion. In the first example we are going to use recursion along with base and recursive case and in the second example, we are going to use sumNat function and third example, we are going to use user-defined tail-recursive function.
Algorithm
Step 1 ? The user defined recursive sumOfNaturalNumbers function is defined as,
For example 1,2 & 3
sumOfNaturalNumbers 0 = 0 sumOfNaturalNumbers n = n + sumOfNaturalNumbers (n - 1).
For example 4
sumNat' 0 acc = acc sumNat' n acc = sumNat' (n-1) (n + acc).
Step 2 ? Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do. In the main function, the value of n is set to 10 and passed to the sumOfNaturalNumbers function, which returns the sum of the natural numbers from 1 to 10.
Step 3 ? The variable named, "n" is being initialized. It will hold the number up to which the sum is to be printed.
Step 4 ? The resultant sum is printed to the console using ?print'' function, after the function is called.
Example 1
In this example, we are going to see that how we can find the sum of natural numbers using recursion. This can be done by using user-defined recursive function.
sumOfNaturalNumbers :: Integer -> Integer sumOfNaturalNumbers 0 = 0 sumOfNaturalNumbers n = n + sumOfNaturalNumbers (n - 1) main :: IO () main = do let n = 10 print (sumOfNaturalNumbers n)
Output
55
Example 2
In this example, we are going to see that how we can find the sum of natural numbers using recursion. This can be done by using user-defined recursive sumNat function.
sumNat :: Integer -> Integer sumNat 0 = 0 sumNat n = n + sumNat (n-1) main :: IO () main = do let num = 5 print (sumNat num)
Output
15
Example 3
In this example, we are going to see that how we can find the sum of natural numbers using recursive case. It uses a function called "sum'" which takes an integer as an argument. The function uses a recursive case, where the base case is when the input integer is 0, in which case the function returns 0. In the other case, when the input integer is greater than 0, the function adds the input integer to the result of the function recursively called with the input integer decremented by 1.
sum' :: Integer -> Integer sum' n | n == 0 = 0 | n > 0 = n + sum' (n-1) main :: IO () main = do let num = 5 print (sum' num)
Output
15
Example 4
In this example, we are going to see that how we can find the sum of natural numbers using recursion. This can be done by using tail-recursive function.
sumNat' :: Integer -> Integer -> Integer sumNat' 0 acc = acc sumNat' n acc = sumNat' (n-1) (n + acc) main :: IO () main = do let num = 10 print (sumNat' num 0)
Output
55
Conclusion
In Haskell, to find the sum of natural numbers using recursion, we can use user-defined functions, case statements or tail-recursive approach.