-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAboutLooping.fs
More file actions
44 lines (34 loc) · 1.23 KB
/
AboutLooping.fs
File metadata and controls
44 lines (34 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
namespace FSharpKoans
open FSharpKoans.Core
//---------------------------------------------------------------
// About Looping
//
// While it's more common in F# to use the Seq, List, or Array
// modules to perform looping operations, you can still fall
// back on traditional imperative looping techniques that you may
// be more familiar with.
//---------------------------------------------------------------
[<Koan(Sort = 12)>]
module ``about looping`` =
[<Koan>]
let LoopingOverAList() =
let values = [0..10]
let mutable sum = 0
for value in values do
sum <- sum + value
AssertEquality sum __
[<Koan>]
let LoopingWithExpressions() =
let mutable sum = 0
for i = 1 to 5 do
sum <- sum + i
AssertEquality sum __
[<Koan>]
let LoopingWithWhile() =
let mutable sum = 1
while sum < 10 do
sum <- sum + sum
AssertEquality sum __
(* NOTE: While these looping constructs can come in handy from time to time,
it's often better to use a more functional approach for looping
such as the functions you learned about in the List module. *)