# Haskell Program to create a simple recursive function

In this article, we are going to understand how to create a simple function in Haskell using user-defined function. Here, the user-defined recursive functions are defined that will contain base case and recursive case. Then, these functions are being called recursively by passing the argument to it.

In all the examples, we are going to define certain functions to perform certain tasks. All these functions include, base and recursive cases. The function defined includes factorial, fibonacci, sumList, etc.

### Algorithm

• Step 1 − The user defined recursive function is defined with base and recursive case.

• 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 user defined recursive function is being called recursively again and again

• Step 3 − The result is printed to the console, after the function is being called recursively.

### Example 1

In this example, the factorial function takes an integer n as an argument and returns the factorial of n. The function is defined using pattern matching, where the first equation factorial 0 = 1 is the base case, and the second equation factorial n = n * factorial (n-1) is the recursive case.

factorial :: Int -> Int
factorial 0 = 1
factorial n = n * factorial (n-1)

main = do
let result = factorial 5
print result


### Output

120


### Example 2

In this example, the fib function takes an integer n as an argument and returns the nth Fibonacci number. The function is defined using pattern matching, where the first two equations fib 0 = 0 and fib 1 = 1 are the base cases, and the third equation fib n = fib (n-1) + fib (n-2) is the recursive case.

fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)

main = do
let result = fib 10
print result


### Output

55


### Example 3

In this example, the sumList function takes a list of integers xs as an argument and returns the sum of its elements. The function is defined using pattern matching, where the first equation sumList [] = 0 is the base case, and the second equation sumList (x:xs) = x + sumList xs is the recursive case.

sumList :: [Int] -> Int
sumList [] = 0
sumList (x:xs) = x + sumList xs

main = do
let result = sumList [1,2,3,4,5]
print result


### Output

15


### Example 4

In this example, a function sumOfNaturalNumbers is defined which takes an integer n as an argument and recursively calls itself until the base case of n = 0 is reached. At each recursive call, the current value of n is added to the sum of the previous values.

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 5

In this example, the gcd' function takes in two integers x and y, and uses recursion to find their GCD. The base case for the recursion is when y is equal to 0, in which case the GCD is simply x. In all other cases, the function calls itself with the arguments y and x mod y, which calculates the remainder of x divided by y. This is done until the remainder is 0, at which point the function will return the GCD.

gcd' :: Int -> Int -> Int
gcd' x y
| y == 0    = x
| otherwise = gcd' y (x mod y)

main :: IO ()
main = do
let x = 48
let y = 18
print (gcd' x y)


### Output

6


### Example 6

In this example, the reverseSentence function takes a string as input and uses recursion to reverse the order of the characters in the string. The base case of the recursion is when the input string is empty, in which case the function returns an empty string. In the recursive case, the function calls itself with the input string minus the first character, and then concatenates the first character of the input string to the end of the reversed string returned by the recursive call.

reverseSentence :: String -> String
reverseSentence [] = []
reverseSentence (x:xs) = reverseSentence xs ++ [x]

main :: IO ()
main = do
let sentence = "Hello World!"
print (reverseSentence sentence)


### Output

"!dlroW olleH"


## Conclusion

In Haskell, recursion allows to find concise and elegant solutions to problems. It is a way of defining a function with base and recursive cases. These functions can take any number of arguments in any form, perform any desired computation, and return the result after being called recursively.