You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
!include`snippetStart="// Simplest possible example of a recursive function:", snippetEnd="// Uncomment at your own risk!"` code/projects/AdvancedRecursion/AdvancedRecursion/Program.cs
20
18
```
21
19
22
20
It is a method (`R`) that simply … calls itself. Even if this method does not "do" anything, calling it will most likely make your program crash, since `R` will keep calling itself forever: this is actually an example of an infinite loop, and the basics of the ["fork bomb" attack](https://en.wikipedia.org/wiki/Fork_bomb)^[Except that the fork bomb calls itself *twice*, and in parallel.].
23
21
24
22
A better definition of recursion would include something about the method eventually terminating, like the following:
As we can see, `Count` itself is *not* recursive, but it calls a recursive method.
45
43
46
44
Finally, methods can be *mutually recursive*: a method `MyTurn` can call a `YourTurn` method that itself calls `MyTurn`. While neither method are recursive, they create a recursive situation, as exemplified below:
Copy file name to clipboardExpand all lines: source/lectures/control/recursion.md
+6-12Lines changed: 6 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,8 +5,6 @@ tags:
5
5
6
6
# Recursion
7
7
8
-
The code for this lecture is available [in this archive](./code/projects/RecursionHelloWorld.zip) (first parts) and [this one](./code/projects/ListDirectoriesFileRecursively.zip) (listing files and folders recursively).
9
-
10
8
## Introduction
11
9
12
10
Recursion is a central notion in programming, simple to state but difficult to master: a method is *recursive* if it calls itself.
@@ -17,7 +15,7 @@ Below, we present some simple recursive programs: while some could be written wi
!include`snippetStart="// Displays numbers from n to 1", snippetEnd="// Displays numbers from 1 to n"` code/projects/RecursionHelloWorld/RecursionHelloWorld/Program.cs
22
20
```
23
21
@@ -43,7 +41,7 @@ In our example, it would display "3 2 1 ".
43
41
44
42
*When* the function calls itself matters a lot. Indeed, consider `displayRAll`, which calls itself *before* executing the `Console.WriteLine` instruction:
!include`snippetStart="// Displays numbers from 1 to n", snippetEnd="// Recursive multiplication"` code/projects/RecursionHelloWorld/RecursionHelloWorld/Program.cs
48
46
```
49
47
@@ -72,7 +70,7 @@ In this example, "1 2 3 " would be displayed: the order is reversed with resp
72
70
73
71
`displayAll` is an example of _tail recursion_: the recursive call is the **last** statement in the method.
74
72
`displayRAll` is an example of _head recursion_: the recursive call is the **first** statement in the method.
75
-
They are furthormore both examples of *linear recursion*, as they call themselves only once.
73
+
They are furthermore both examples of *linear recursion*, as they call themselves only once.
76
74
77
75
## Recursive Methods Returning a Value
78
76
@@ -83,7 +81,7 @@ Recursive methods can also return a value, used by previous calls to compute som
83
81
For example, consider that multiplication can be defined by addition: indeed, $x × y$ is $y + y + y + … + y$ where $y$ is summed $x$ times. Stated differently (read: recursively), $x × y$ is $y + ((x - 1) × y)$.
!include`snippetStart="// Recursive factorial", snippetEnd="// An improvment would be to replace"` code/projects/RecursionHelloWorld/RecursionHelloWorld/Program.cs
`snippetStart="// https://stackoverflow.com/a/929277", snippetEnd="// An improvment would be to replace"
116
-
-->
117
-
118
112
Note that our previous examples were calling themselves only once per method call, but that `ListDir` calls itself as many times as there are folders in the folder currently examined.
Copy file name to clipboardExpand all lines: source/projects/file_displayer.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,7 +23,7 @@ A bonus (for more experienced programmers) asks to compute the winner of a race
23
23
24
24
#### In more details
25
25
26
-
Your goal is to design and implement a static `TextFileHelper` class containing 5 methods (the fifth one being optional):
26
+
Your goal is to design and implement a static `FileDisplayer` class containing 5 methods (the fifth one being optional):
27
27
28
28
#. A `Display` method that takes a `string` as an argument and displays the content of the file located at the corresponding path if it exists, and an error message otherwise.
29
29
#. A `DisplayN` method that takes a `string` and an `int`, and displays the line corresponding to the `int` argument from the file located at the corresponding path if it exists (with a line number before), and an error message otherwise.
0 commit comments