concept recursion in category c#

appears as: recursion, recursion, The recursion
Real-World Functional Programming with Examples in F# and C#

This is an excerpt from Manning's book Real-World Functional Programming with Examples in F# and C#.

In this case, we can’t directly replace the variable with value bindings, because we need to modify the value during every evaluation of the loop. The program has to keep a certain state, and that state changes on each iteration of the loop. That means we can’t declare a new value for every change of the state, as we did in our earlier example. We need to make a fundamental change in the code and use a technique called recursion instead of using loops:

int SumNumbers(int from, int to) {
if (from > to) return 0;
int sumRest = SumNumbers(from + 1, to);
return from + sumRest;
}

As you know, recursion means that a function (SumNumbers in our case) calls itself—in this case, when we calculate the value of the sumRest variable. In this code we’re using only value bindings, so it’s purely functional. The state of the computation, which was originally stored in a mutable variable, is now expressed using recursion. When we first mentioned that we can’t declare a new variable for every change of the state, we were in some sense incorrect, because that’s what our new recursive implementation does. Every time the function recursively calls itself, it skips the first number and calculates a sum of the remaining numbers. This result is bound to a variable sumRest, which is declared as a new variable during every execution of the recursive function.

Writing the recursive part of a computation every time would be difficult, so functional languages provide a way for “hiding” the difficult recursive part and expressing most of the problems without explicitly using recursion. We’ll get back to this topic in section 2.3.1 after we finish our discussion of calculation in functional programs.

Since recursion is absolutely essential for functional programming, functional languages have developed several ways for avoiding stack overflows even for very deep recursive calls and some other optimization mechanisms. This and other advanced topics will be discussed in chapter 10.

3.3. Lists and recursion

A tuple is a very good example of an immutable functional data type, but there’s one more property of many functional data types that’s worth discussing in this chapter: recursion. Let’s start with a classic programming joke: what’s the dictionary definition of recursion? “Recursion. See recursion.”

Recursion appears in functional programming in different forms. It can be present in the structure of the type, such as lists. The type that represents functional lists is either an empty or is composed from an element and a list. You can see that the type list that we’re describing is recursively used in its definition. The second form of recursion is probably more widely know and is used when writing recursive functions. Let’s start by looking at one example of the second form; then we’ll focus on lists to demonstrate the first form.

Listing 12.5. Generating an infinite sequence of random colors in C# and F#
// C# version using loops IEnumerable<Color> RandomColors(){ var rnd = new Random(); while(true) { ❶ int r = rnd.Next(256), g = rnd.Next(256), b = rnd.Next(256); yield return Color.FromArgb(r, g, b); ❷ } } // F# version using recursion let rnd = new Random() let rec randomColors = seq { ❸ let r, g, b = rnd.Next(256), rnd.Next(256), rnd.Next(256) yield Color.FromArgb(r, g, b) ❹ yield! randomColors } ❺
Listing 14.23. Running simulation using recursion and async (F#)