Haskell Program to Find Sum of N Numbers Using Recursion


In Haskell, we can find Sum of N Numbers by using recursion, tail-recursion and fold-recursion. In the first example we are going to use base case, (sum_n [] = 0) and recursive case, (sum_n (x:xs) = x + sum_n xs)) and in second example, we are going to use, tail-recursion. And in the third example, we are going to use (sumOfN''' xs = foldr (+) 0 xs) function.

Algorithm

  • Step 1 − The recursive sum_n function is defined as,

  • For example 1 −

sum_n [] = 0
sum_n (x:xs) = x + sum_n xs.
  • For example 2 −

sumOfN' acc 0 = acc
sumOfN' acc n = sumOfN' (acc + n) (n-1).
  • For example 3 −

sumOfN''' xs = foldr (+) 0 xs.
  • 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, we create a list of numbers and pass it to the sum_n function to find the sum. The result is then printed.

  • Step 3 − The variable named, “numbers” is being initialized. It will hold the list of numbers whose sum is to be computed.

  • Step 4 − The resultant sum of the numbers is printed to the console using ‘print’ function, after the sum_n function is called.

Example 1

In this example, the function sum_n takes a list of integers as an input and uses a pattern matching to check if the list is empty. If it is, it returns 0.

If the list is not empty, it takes the first element (x) and adds it to the sum of the rest of the list (xs). This is done using the sum_n function recursively.

sum_n :: [Integer] -> Integer
sum_n [] = 0
sum_n (x:xs) = x + sum_n xs

main :: IO ()
main = do
   let numbers = [1,2,3,4,5]
   print (sum_n numbers)

Output

15

Example 2

In this example, a tail-recursive function sumOfN' is defined that takes in two arguments: an accumulator acc and n. The base case is when n equals 0, in which case the function returns the value of acc. In all other cases, the function returns the result of calling sumOfN' with the arguments acc + n and n-1. The function uses an accumulator to keep track of the sum and avoid stack overflow.

sumOfN' :: (Eq a, Num a) => a -> a -> a
sumOfN' acc 0 = acc
sumOfN' acc n = sumOfN' (acc + n) (n-1)

main = print (sumOfN' 0 5)

Output

15

Example 3

In this example, the foldr function is used to find the sum of elements in the list. The foldr function takes a binary function, a list, and applies the binary function to the elements of the list, folding the list up into a single value. In this case, the binary function is (+) and the initial value is 0, so it add the elements of the list one by one starting from the right side.

sumOfN''' :: (Num a) => [a] -> a
sumOfN''' xs = foldr (+) 0 xs

main = print (sumOfN''' [1,2,3,4,5])

Output

15

Conclusion

In Haskell, the sum of n numbers can be calculated using recursion with user defined function, or by using tail or fold recursive approaches.

Updated on: 27-Mar-2023

422 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements