Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Immutable values in F#

Tags:

f#

I'm just getting started in F# and have a basic question.

Here's the code:

let rec forLoop body times =
    if times <= 0 then
        ()
    else
        body()
        forLoop body (times - 1)

I don't get the concept of how when you define a variable it is a value and immutable. Here, the value is changing in order to loop. How is that different from a variable in C#?

like image 994
rsteckly Avatar asked Dec 17 '22 01:12

rsteckly


1 Answers

it is not changing. you use recursion. that variable remains unchanged, but it is subtracted one and passed to function. function is same in this case.

stack will look like

forLoop body 0
 |
 forLoop body 1
   |
   forLoop body 2
like image 92
Andrey Avatar answered Jan 03 '23 13:01

Andrey