Recursion in Erlang is a powerful technique that allows a function to call itself repeatedly until a certain condition is met. Here's a brief overview of how to use recursion in Erlang:

**Define a base case**: Every recursive function needs a base case, which specifies when the recursion should stop. This base case is typically an if statement or pattern match that checks for a specific condition. When this condition is met, the recursion stops and the result is returned.**Define the recursive case**: In addition to the base case, you need to define the recursive case, which describes how the function should be called again with a modified input. In the recursive case, you perform some operation or computation on the input and call the function again with the modified input.**Modify the input**: In each recursive call, you need to modify the input in some way to make progress towards the base case. This can be done by either reducing the input or changing some of its properties. The modified input is then used in the subsequent recursive call.**Combine the results**: If the recursive function needs to combine the results of multiple recursive calls, you can do this by storing the results in a list or accumulator variable and updating it in each recursive call. Once the base case is reached, the accumulated results can be returned.**Properly handle tail recursion**: Erlang supports tail recursion optimization, which means that recursive calls made in the tail position of a function can be optimized to avoid stack overflow. To ensure proper tail recursion, make sure that the recursive call is the last operation in your function and there are no pending operations to be performed afterwards.

By following these steps, you can effectively use recursion in Erlang to solve various problems and perform complex computations. It's important to understand the termination condition and how the input is modified in each recursive call to avoid infinite recursion.

## What is the maximum depth of recursion in Erlang?

The maximum depth of recursion in Erlang is limited by the available stack size for each process. By default, each process has a stack size of 1MB, which means the maximum depth of recursion is limited to approximately 1 million function calls. However, this limit can be changed by increasing the stack size using the "+P" command-line option or by using the erlang:system_flag/2 function to set the stack size for a specific process.

## How to avoid stack overflow in recursive functions in Erlang?

To avoid stack overflow in recursive functions in Erlang, you can use following techniques:

**Tail recursion**: Instead of calling the recursive function before returning, use an accumulator parameter to pass the intermediate results. This allows the function to be optimized by the Erlang runtime system, resulting in constant stack usage.

Example:

1 2 3 4 |
sum_list(List) -> sum_list(List, 0). sum_list([], Acc) -> Acc; sum_list([H|T], Acc) -> sum_list(T, Acc + H). |

**Use tail-recursive libraries**: There are libraries available in Erlang like lists, lists:foldl/3, lists:foldr/3 which provide tail-recursive alternatives to common recursive functions.

Example:

```
1
``` |
```
Sum = lists:foldl(fun(X, Acc) -> X + Acc end, 0, List).
``` |

**Divide and conquer**: For certain problems, you can divide the task into smaller subtasks, reducing the depth of recursion. However, this approach may not be suitable for all problems.

Example:

1 2 3 4 5 6 |
sum_list([]) -> 0; sum_list(List) -> sum_list(split_list(List)). split_list([]) -> []; split_list([H1,H2|T]) -> [H1+H2 | split_list(T)]; split_list([H]) -> [H]. |

**Use tail recursion optimizations**: Some versions of Erlang provide tail recursion optimizations, like the +native or +native +{hipe, [o3]} compile options. These options can improve the performance and increase the stack size available for recursion.

Note: The above techniques may not completely eliminate stack overflow in all cases, as Erlang still has a limited stack size. If your problem requires deep recursion, you may need to consider an iterative or non-recursive solution instead.

## How to use recursion to find the length of a list in Erlang?

To find the length of a list using recursion in Erlang, you can create a function that iterates through the list, incrementing a counter until you reach the end. Here's an example implementation:

1 2 3 4 5 6 7 8 9 10 |
-module(list_length). -export([length/1]). length(List) -> length(List, 0). length([], Count) -> Count; length([_|Rest], Count) -> length(Rest, Count+1). |

In this example, the `length/1`

function is the initial entry point which calls the `length/2`

function with an initial count of 0.
The `length/2`

function has two clauses:

- The first clause matches an empty list [] and returns the current count.
- The second clause matches a list with a head element [_|Rest] and recursively calls length/2 with the remaining elements (Rest) and an incremented count.

By successively passing the tail of the list and incrementing the count, the length of the list can be computed.