DEV Community

Cover image for F# For Dummys - Day 9 Branching
AllenZhu
AllenZhu

Posted on • Edited on

F# For Dummys - Day 9 Branching

Today we learn control flow, or branching
sometimes we need to execute a block of code only when it meets some condition

//Pseudocode
if conditionOne then doTaskOne
else if conditionTwo then doTaskTwo
else doSomethingElse
Enter fullscreen mode Exit fullscreen mode

we introduce 2 ways of branching

if...then

let temperature = 15

if temperature < 0 then
    printfn "Wear down coat"
elif temperature < 10 then
    printfn "Wear sweater"
elif temperature < 20 then
    printfn "Wear long sleeves"
elif temperature < 30 then
    printfn "Wear T-shirt"
else
    printfn "Wear bikini"
Enter fullscreen mode Exit fullscreen mode

temperature = 15, the first branch suits is temperature < 20 , even though temperature < 30 is also true, the rest logic will not execute

match...with

// Match expression. 
match test-expression with
| pattern1 [ when condition ] -> result-expression1
| pattern2 [ when condition ] -> result-expression2
| ...
Enter fullscreen mode Exit fullscreen mode

equivalent example:

let temperature = 15

match temperature with
| temperature when temperature < 0 -> printfn "Wear down coat"
| temperature when temperature < 10 -> printfn "Wear sweater"
| temperature when temperature < 20 -> printfn "Wear long sleeves"
| temperature when temperature < 30 -> printfn "Wear T-shirt"
| _ -> printfn "Wear bikini"
Enter fullscreen mode Exit fullscreen mode

AND Pattern: we can combine conditions with (&&) operator, it hits when both condition match

let age = 10

match age with
| age when age > 0 && age <= 1 -> printfn "Infant"
| age when age > 1 && age <= 12 -> printfn "Child"
| age when age > 12 && age <= 16 -> printfn "Teenager"
| age when age > 16 -> printfn "Adults"
| _ -> printfn "Not born"
Enter fullscreen mode Exit fullscreen mode

OR Pattern: we can combine patterns using the (||) operator to match multiple patterns

let x = 10

match x with
| x when x > 0 || x = 0 -> printfn "Positive or Zero"
| _ -> printfn "Negative"
Enter fullscreen mode Exit fullscreen mode

we can define a pattern matching function

let temperature = 15

let clothingAdvice temp =
    match temp with
    | t when t < 0 -> "Wear down coat"
    | t when t < 10 -> "Wear sweater"
    | t when t < 20 -> "Wear long sleeves"
    | t when t < 30 -> "Wear T-shirt"
    | _ -> "Wear bikini"

printfn "%s" (clothingAdvice temperature)
Enter fullscreen mode Exit fullscreen mode

another example from windows documentation

type Shape =
    | Square of side: double
    | Rectangle of width: double * length: double

let getArea shape =
    match shape with
    | Square side -> side * side
    | Rectangle (width, length) -> width * length

let square = Square 2.0
printfn $"The area of the square is {getArea square}"
let rect = Rectangle (3.0, 2.0)
printfn $"The area of the rect is {getArea rect}"
Enter fullscreen mode Exit fullscreen mode

getArea function calculate the area according to different shape
for a Square, it use side * side = 2.0 * 2.0
for a Rectangle, it use width * length = 3.0 * 2.0

The area of the square is 4
The area of the rect is 6
Enter fullscreen mode Exit fullscreen mode

we didn't learn type yet, it defines a Union type here, and there is more powerful usage of match, we will introduce type, Union and discover more usage of match in the coming days

Top comments (0)