Skip to content

Commit 2a42d73

Browse files
committed
Fibonacci
1 parent 5b3a7da commit 2a42d73

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

source_md/recursion.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ Recursion is actually a way of defining functions in which the function is appli
1717
Definitions in mathematics are often given recursively.
1818
For instance, the fibonacci sequence is defined recursively.
1919
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.
2121
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!
2828

2929
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.
3030
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

Comments
 (0)