F# For Dummys - Day 10 Loop
Tags: fsharp
Published: 2024-05-21 12:12:33

Today we learn loop, do a task repeatly
#### What is loop
loop is used to repeat a block of code multiple times</br>
we need to set when to start, how many times it repeats or condition to end loops</br>
a loop never ends is called infinite loop
#### Three ways to loop
- for loop</br>
Iterates over a specified range of values. The loop variable takes each value in the range one by one
```f#
// Forward for loop
for i = 1 to 5 do
    printfn "Jim has run %d laps" i

// Reverse for loop
for i = 5 downto 1 do
    printfn "Days until birthday: %d days left" (i - 1)
```
- while loop</br>
Repeatedly executes the block of code as long as the condition remains true
```f#
let mutable counter = 1
while counter <= 5 do
    printfn "Jim has run %d laps" counter
    counter <- counter + 1
```
- recursive function</br>
Calls itself with updated parameters</br>

syntax: let rec funcName
```f#
let rec recursiveLoop n =
    if n > 0 then
        printfn "Jim run 1 lap: %d laps left" (n - 1)
        recursiveLoop (n - 1)

recursiveLoop 5
```
Understanding recursive function:

Imagine you have a set of [Russian dolls](https://www.quora.com/What-is-a-Russian-doll), where each doll contains a smaller doll inside it

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cpxmejowt131vlev0dxx.png)

To find the smallest doll, you would:
1. Open the current doll
2. Check if there is another smaller doll inside
3. If there is, repeat the process with the smaller doll
```f#
// Pseudocode
let findSmallestDoll doll =
    if doll contains no smaller doll then
        return doll
    else
        let innerDoll = open(doll) // smaller doll inside
        findSmallestDoll(innerDoll)
```
#### How to choose
- for loop is simple and easy to understand when iterating over a known range of values</br>
- while loop is useful when the number of iterations is not known beforehand</br>
- recursive function making code more expressive and often easier to reason about</br>

---
exercises:</br>
1. 1 + 2 + 3 + ... + 99 + 100 = ?
2. 1 + 3 + 5 + ... + 97 + 99 = ?


answers:</br>
question 1
- for
```f#
let mutable sum = 0
for i = 1 to 100 do
    sum <- sum + i
printfn "Sum using for loop: %d" sum
```
- while
```f#
let mutable sum = 0
let mutable counter = 1
while counter <= 100 do
    sum <- sum + counter
    counter <- counter + 1
printfn "Sum using while loop: %d" sum
```
- recursive function
```f#
let rec sumRecursive n acc =
    if n = 0 then acc
    else sumRecursive (n - 1) (acc + n)

printfn "Sum using recursive function: %d" (sumRecursive 100 0)
```
question 2
- for
```f#
let mutable sum = 0
for i = 1 to 100 do
    if i % 2 <> 0 then
        sum <- sum + i
printfn "Sum using for loop: %d" sum
```
- while
```f#
let mutable sum = 0
let mutable counter = 1
while counter <= 100 do
    if counter % 2 <> 0 then
        sum <- sum + counter
    counter <- counter + 1
printfn "Sum using while loop: %d" sum
```
- recursive function
```f#
let rec sumOddRecursive n acc =
    if n = 0 then acc
    else 
        let newAcc = if n % 2 <> 0 then acc + n else acc
        sumOddRecursive (n - 1) newAcc

printfn "Sum using recursive function: %d" (sumOddRecursive 100 0)
```