-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAboutLists.fs
More file actions
127 lines (94 loc) · 3.93 KB
/
AboutLists.fs
File metadata and controls
127 lines (94 loc) · 3.93 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
namespace FSharpKoans
open FSharpKoans.Core
open System.Collections.Generic
//---------------------------------------------------------------
// About Lists
//
// Lists are important building blocks that you'll use frequently
// in F# programming. They are used to group arbitrarily large
// sequences of values. It's very common to store values in a
// list and perform operations across each value in the
// list.
//---------------------------------------------------------------
[<Koan(Sort = 9)>]
module ``about lists`` =
[<Koan>]
let CreatingLists() =
let list = ["apple"; "pear"; "grape"; "peach"]
//Note: The list data type in F# is a singly linked list,
// so indexing elements is O(n).
AssertEquality list.Head __
AssertEquality list.Tail __
AssertEquality list.Length __
(* .NET developers coming from other languages may be surprised
that F#'s list type is not the same as the base class library's
List<T>. In other words, the following assertion is true *)
let dotNetList = new List<string>()
//you don't need to modify the following line
AssertInequality (list.GetType()) (dotNetList.GetType())
[<Koan>]
let BuildingNewLists() =
let first = ["grape"; "peach"]
let second = "pear" :: first
let third = "apple" :: second
//Note: "::" is known as "cons"
AssertEquality ["apple"; "pear"; "grape"; "peach"] third
AssertEquality second __
AssertEquality first __
//What happens if you uncomment the following?
//first.Head <- "apple"
//first.Tail <- ["peach"; "pear"]
//THINK ABOUT IT: Can you change the contents of a list once it has been
// created?
[<Koan>]
let ConcatenatingLists() =
let first = ["apple"; "pear"; "grape"]
let second = first @ ["peach"]
AssertEquality first __
AssertEquality second __
(* THINK ABOUT IT: In general, what performs better for building lists,
:: or @? Why?
Hint: There is no way to modify "first" in the above example. It's
immutable. With that in mind, what does the @ function have to do in
order to append ["peach"] to "first" to create "second"? *)
[<Koan>]
let CreatingListsWithARange() =
let list = [0..4]
AssertEquality list.Head __
AssertEquality list.Tail __
[<Koan>]
let CreatingListsWithComprehensions() =
let list = [for i in 0..4 do yield i ]
AssertEquality list __
[<Koan>]
let ComprehensionsWithConditions() =
let list = [for i in 0..10 do
if i % 2 = 0 then yield i ]
AssertEquality list __
[<Koan>]
let TransformingListsWithMap() =
let square x =
x * x
let original = [0..5]
let result = List.map square original
AssertEquality original __
AssertEquality result __
[<Koan>]
let FilteringListsWithFilter() =
let isEven x =
x % 2 = 0
let original = [0..5]
let result = List.filter isEven original
AssertEquality original __
AssertEquality result __
[<Koan>]
let DividingListsWithPartition() =
let isOdd x =
x % 2 <> 0
let original = [0..5]
let result1, result2 = List.partition isOdd original
AssertEquality result1 __
AssertEquality result2 __
(* Note: There are many other useful methods in the List module. Check them
via intellisense in Visual Studio by typing '.' after List, or online at
https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/lists *)