This project is read-only.
For Loops

Category:
For and While Loops
Description: Some simple 'for' loops
Code:
let SampleForLoop1() =
for i in ["quick"; "brown"; "fox" ] do
printfn "i = %s" i

Execution Result:
i = quick
i = brown
i = fox

For Loops

Category:
For and While Loops
Description: Some simple 'for' loops
Code:
let SampleForLoop2() =
for i in 1 .. 10 do
printfn "In a for-loop, i = %d" i

Execution Result:
In a for-loop, i = 1
In a for-loop, i = 2
In a for-loop, i = 3
In a for-loop, i = 4
In a for-loop, i = 5
In a for-loop, i = 6
In a for-loop, i = 7
In a for-loop, i = 8
In a for-loop, i = 9
In a for-loop, i = 10

For Loops

Category:
For and While Loops
Description: Some simple nested 'for' loops
Code:
let SampleForLoop3() =
for i in 0 .. 9 do
for j = 0 to i-1 do
printfn " "
for j = i to 9 do
printfn "%d" j
printfn ""

Execution Result:
0
1
2
3
4
5
6
7
8
9


1
2
3
4
5
6
7
8
9



2
3
4
5
6
7
8
9




3
4
5
6
7
8
9





4
5
6
7
8
9






5
6
7
8
9







6
7
8
9








7
8
9









8
9










9


While Loops

Category:
For and While Loops
Description: A simple 'while' loop that counts, skipping by 2
Code:
let SampleWhileLoop1() =
let count = ref 0
while (!count < 10) do
printfn "Counting, skipping by 2, count = %d..." !count;
count := !count + 2
printfn "Done counting!"

Execution Result:
Counting, skipping by 2, count = 0...
Counting, skipping by 2, count = 2...
Counting, skipping by 2, count = 4...
Counting, skipping by 2, count = 6...
Counting, skipping by 2, count = 8...
Done counting!

While Loops

Category:
For and While Loops
Description: A simple 'while' loops that busy-waits until the given time-span has passed
Code:
let SampleWhileLoop2() =
let start = DateTime.Now
let duration = System.TimeSpan.FromMilliseconds(8.0)
let diff (a:DateTime) (b:DateTime) = System.DateTime.op_Subtraction(System.DateTime.Now,b)
printfn "Waiting..."

// Here's the loop
while diff DateTime.Now start < duration do
printfn "."

// OK, we're done...
let span = diff DateTime.Now start
printfn "\nAttempted to busy-wait 8ms, actually waited %dms" span.Milliseconds

//--------------------------------------------------------

Execution Result:
Waiting...
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Attempted to busy-wait 8ms, actually waited 8ms

Last edited Sep 13, 2011 at 12:29 AM by ttliu2000, version 2

Comments

No comments yet.