Logo

dev-resources.site

for different kinds of informations.

F# For Dummys - Day 10 Loop

Published at
5/21/2024
Categories
fsharp
Author
pythonzhu
Categories
1 categories in total
fsharp
open
Author
9 person written this
pythonzhu
open
F# For Dummys - Day 10 Loop

Today we learn loop, do a task repeatly

What is loop

loop is used to repeat a block of code multiple times
we need to set when to start, how many times it repeats or condition to end loops
a loop never ends is called infinite loop

Three ways to loop

  • for loop Iterates over a specified range of values. The loop variable takes each value in the range one by one
// 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)
Enter fullscreen mode Exit fullscreen mode
  • while loop Repeatedly executes the block of code as long as the condition remains true
let mutable counter = 1
while counter <= 5 do
    printfn "Jim has run %d laps" counter
    counter <- counter + 1
Enter fullscreen mode Exit fullscreen mode
  • recursive function Calls itself with updated parameters

syntax: let rec funcName

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

recursiveLoop 5
Enter fullscreen mode Exit fullscreen mode

Understanding recursive function:

Imagine you have a set of Russian dolls, where each doll contains a smaller doll inside it

Image description

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
// Pseudocode
let findSmallestDoll doll =
    if doll contains no smaller doll then
        return doll
    else
        let innerDoll = open(doll) // smaller doll inside
        findSmallestDoll(innerDoll)
Enter fullscreen mode Exit fullscreen mode

How to choose

  • for loop is simple and easy to understand when iterating over a known range of values
  • while loop is useful when the number of iterations is not known beforehand
  • recursive function making code more expressive and often easier to reason about

exercises:

  1. 1 + 2 + 3 + ... + 99 + 100 = ?
  2. 1 + 3 + 5 + ... + 97 + 99 = ?

answers:
question 1

  • for
let mutable sum = 0
for i = 1 to 100 do
    sum <- sum + i
printfn "Sum using for loop: %d" sum
Enter fullscreen mode Exit fullscreen mode
  • while
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
Enter fullscreen mode Exit fullscreen mode
  • recursive function
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)
Enter fullscreen mode Exit fullscreen mode

question 2

  • for
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
Enter fullscreen mode Exit fullscreen mode
  • while
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
Enter fullscreen mode Exit fullscreen mode
  • recursive function
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)
Enter fullscreen mode Exit fullscreen mode
fsharp Article's
30 articles in total
Favicon
Learning some Fantomas AST
Favicon
Ingesting Data in F# with Aether: A Practical Guide to Using Lenses, Prisms, and Morphisms
Favicon
Suicide Boys Merch quality designed shop
Favicon
F# 9: Nullable Reference Types and Advancing Null Safety
Favicon
Unlocking High-Performance AI Computing with F#: A Comprehensive Guide
Favicon
Scope progression
Favicon
How do I register a complaint with Delhivery?
Favicon
New wallpapers every day
Favicon
F# 🤝 GTK4
Favicon
Introducing F# with Semantic Kernel: Simplifying AI App Development with the Pipeline Pattern
Favicon
Describing musical domain with F#
Favicon
Who's Your .NET Ally? - F# vs C#
Favicon
F# For Dummys - Day 13 Collections Array
Favicon
F# For Dummys - Day 12 Collections List
Favicon
Primitive Type Differentiation in F#
Favicon
Request -> Handler -> SubPub Pattern with MediatR or Raw F# code
Favicon
F# For Dummys - Day 11 Collections Tuple
Favicon
F# For Dummys - Day 9 Branching
Favicon
F# For Dummys - Day 8 Function && Pipeline
Favicon
F# For Dummys - Day 7 Operators
Favicon
F# For Dummys - Day 16 Collections Sequence
Favicon
F# For Dummys - Day 15 Collections Set
Favicon
F# For Dummys - Day 14 Collections Map
Favicon
F# For Dummys - Day 10 Loop
Favicon
F# For Dummys - Day 6 Format
Favicon
F# For Dummys - Day 5 Mutable
Favicon
F# For Dummys - Day 4 Value
Favicon
F# For Dummys - Day 3 New Program
Favicon
F# For Dummys - Day 2 Environment
Favicon
F# For Dummys - Day 0 Foreword

Featured ones: