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
Copy file name to clipboardExpand all lines: source_md/recursion.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,14 +17,14 @@ Recursion is actually a way of defining functions in which the function is appli
17
17
Definitions in mathematics are often given recursively.
18
18
For instance, the fibonacci sequence is defined recursively.
19
19
First, we define the first two fibonacci numbers non-recursively.
20
-
We say that *F(0) = 0* and *F(1) = 1*, meaning that the 0th and 1st fibonacci numbers are 0 and 1, respectively.
20
+
We say that $F(0) = 0$ and $F(1) = 1$, meaning that the 0<sup>th</sup> and 1<sup>st</sup> fibonacci numbers are $0$ and $1$, respectively.
21
21
Then we say that for any other natural number, that fibonacci number is the sum of the previous two fibonacci numbers.
22
-
So *F(n) = F(n-1) + F(n-2)*.
23
-
That way, *F(3)* is *F(2) + F(1)*, which is *(F(1) + F(0)) + F(1)*.
24
-
Because we've now come down to only non-recursively defined fibonacci numbers, we can safely say that *F(3)* is 2.
25
-
Having an element or two in a recursion definition defined non-recursively (like *F(0)* and *F(1)* here) is also called the **edge condition** and is important if you want your recursive function to terminate.
26
-
If we hadn't defined *F(0)* and *F(1)* non recursively, you'd never get a solution any number because you'd reach 0 and then you'd go into negative numbers.
27
-
All of a sudden, you'd be saying that *F(-2000)* is *F(-2001) + F(-2002)* and there still wouldn't be an end in sight!
22
+
So $F(n) = F(n-1) + F(n-2)$.
23
+
That way, $F(3)$ is $F(2) + F(1)$, which is $(F(1) + F(0)) + F(1)$.
24
+
Because we've now come down to only non-recursively defined fibonacci numbers, we can safely say that $F(3)$ is $2$.
25
+
Having an element or two in a recursion definition defined non-recursively (like $F(0)$ and $F(1)$ here) is also called the **edge condition** and is important if you want your recursive function to terminate.
26
+
If we hadn't defined $F(0)$ and $F(1)$ non recursively, you'd never get a solution any number because you'd reach $0$ and then you'd go into negative numbers.
27
+
All of a sudden, you'd be saying that $F(-2000)$ is $F(-2001) + F(-2002)$ and there still wouldn't be an end in sight!
28
28
29
29
Recursion is important to Haskell because unlike imperative languages, you do computations in Haskell by declaring what something *is* instead of declaring *how* you get it.
30
30
That's why there are no while loops or for loops in Haskell and instead we many times have to use recursion to declare what something is.
0 commit comments