There are 3 main loop types in F# for imperative programming style:
- Iteration
for id = … to … do - Sequence loop
for … in expression do - Pre-test loop
while … do
This is an example code for each loops:
#light /// Iteration printfn "for to do" printf "[" for i=1 to 5 do printf "%d " i printf "]" /// Sequence loop printfn "" printfn "" printfn "for in do" let items = seq{1..5} printf "[" for i in items do printf "%d " i printf "]" /// Pre-test loop printfn "" printfn "" printfn "while do" printf "[" let mutable i = 1 while i<=5 do printf "%d " i i <- i+1 printf "]"
for to do [1 2 3 4 5 ] for in do [1 2 3 4 5 ] while do [1 2 3 4 5 ] |
All the loop types can be found in C#.
seq{1..5} is a sequence data type that means:
val it : seq<int> = seq [1; 2; 3; 4; ...] |
So we can say it contains 1,2,3,4,5.
The
let mutable i = 1
line means variable i can change its value. So i <- i+1 means assignment and can be evaluated.
Great posst thanks
ReplyDelete