Skip to content

Commit caf82b1

Browse files
cuihtlauacchristinerose
authored andcommitted
Apply suggestions from code review
Co-authored-by: Christine Rose <christinerose@users.noreply.github.com>
1 parent 7de3776 commit caf82b1

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

data/tutorials/language/3ds_03_set.md

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,26 @@ pay attention to the case; you need to type `Set.Make(String)` and not
2222
`Set.Make(string)`. The reason behind this is explained in the
2323
"Technical Details" section at the bottom.
2424

25-
Doing this in the OCaml's top level will yield a lot of output:
25+
Doing this in the OCaml's toplevel will yield a lot of output:
2626

2727
```ocamltop
2828
module SS = Set.Make(String);;
2929
```
3030

31-
What happened here is that after assigning your newly created module to the name
32-
`SS`, OCaml's top level then displayed the module, which in this case contains
31+
What happened here is that after assigning your newly-created module to the name
32+
`SS`, OCaml's toplevel displayed the module, which in this case contains
3333
a large number of convenience functions for working with sets (for example `is_empty`
34-
for checking if you set is empty, `add` to add an element to your set, `remove` to
34+
to check if your set is empty, `add` to add an element to your set, `remove` to
3535
remove an element from your set, and so on).
3636

37-
Note also that this module defines two types: `type elt = String.t` representing
38-
the type of the elements, and `type t = Set.Make(String).t` representing the type of
39-
the set itself. It's important to note this, because these types are used in the
37+
Note also that this module defines two types: `type elt = String.t`, representing
38+
the type of the elements, and `type t = Set.Make(String).t`, representing the type of
39+
the set itself. It's important to note this because these types are used in the
4040
signatures of many of the functions defined in this module.
4141

4242
For example, the `add` function has the signature `elt -> t -> t`, which means
4343
that it expects an element (a String), and a set of strings, and will return to you
44-
a set of strings. As you gain more experience in OCaml and other function languages,
44+
a set of strings. As you gain more experience in OCaml and other functional languages,
4545
the type signature of functions are often the most convenient form of documentation
4646
on how to use those functions.
4747

@@ -54,30 +54,30 @@ find what function or value you should use to do this, but this is an excellent
5454
opportunity to practice reading the type signatures and inferring the answer from them.
5555

5656
You want to create a new set (as opposed to modifying an existing set). So you should
57-
look for functions whose return result has type `t` (the type representing the set),
57+
look for functions whose return result has type `t` (the type representing the set)
5858
and which *does not* require a parameter of type `t`.
5959

6060
Skimming through the list of functions in the module, there's only a handful of functions
6161
that match that criteria: `empty: t`, `singleton : elt -> t`, `of_list : elt list -> t`
6262
and `of_seq : elt Seq.t -> t`.
6363

6464
Perhaps you already know how to work with lists and sequences in OCaml or
65-
perhaps you don't. For now, let's assume you don't know, and so we'll focus
65+
perhaps you don't. For now, let's assume you don't know, so we'll focus
6666
our attention on the first two functions in that list: `empty` and `singleton`.
6767

68-
The type signature for `empty` says that it simply returns `t`, i.e. an instance
68+
The type signature for `empty` says that it simply returns `t`, i.e., an instance
6969
of our set, without requiring any parameters at all. By intuition, you might
7070
guess that the only reasonable set that a library function could return when
7171
given zero parameters is the empty set. And the fact that the function is named
7272
`empty` reinforces this theory.
7373

74-
Is there a way to test this theory? Perhaps if we had a function which
74+
Is there a way to test this theory? Perhaps if we had a function that
7575
could print out the size of a set, then we could check if the set we get
7676
from `empty` has a size of zero. In other words, we want a function which
77-
receives a set as a parameter, and returns an integer as a result. Again,
77+
receives a set as a parameter and returns an integer as a result. Again,
7878
skimming through the list of functions in the module, we see there is a
7979
function which matches this signature: `cardinal : t -> int`. If you're
80-
not familiar with the word "cardinal", you can look it up on Wikipedia
80+
not familiar with the word "cardinal," you can look it up on Wikipedia
8181
and notice that it basically refers to the size of sets, so this reinforces
8282
the idea that this is exactly the function we want.
8383

@@ -88,17 +88,17 @@ let s = SS.empty;;
8888
SS.cardinal s;;
8989
```
9090

91-
Excellent, it looks like `SS.empty` does indeed create an empty set,
91+
Excellent! It looks like `SS.empty` does indeed create an empty set,
9292
and `SS.cardinal` does indeed print out the size of a set.
9393

9494
What about that other function we saw, `singleton : elt -> t`? Again,
9595
using our intuition, if we provide the function with a single element,
9696
and the function returns a set, then probably the function will return
9797
a set containing that element (or else what else would it do with the
9898
parameter we gave it?). The name of the function is `singleton`, and
99-
again if you're unfamiliar with what word, you can look it up on
100-
Wikipedia and see that the word means "a set with exactly one element".
101-
It sounds like we're on the right track again. Let's test our theory.
99+
if you're unfamiliar with what word, you can look it up on
100+
Wikipedia and see that the word means "a set with exactly one element."
101+
It sounds like we're on the right track, so let's test our theory.
102102

103103
```ocamltop
104104
let s = SS.singleton "hello";;
@@ -110,12 +110,12 @@ It looks like we were right again!
110110
## Working with Sets
111111

112112
Now let's say we want to build bigger and more complex sets. Specifically,
113-
let's say we want to add another element to our existing set. So we're
114-
looking for a function with two parameters: One of the parameters should
113+
let's say we want to add another element to our existing set, so we're
114+
looking for a function with two parameters. One of the parameters should
115115
be the element we wish to add, and the other parameter should be the set
116116
that we're adding to. For the return value, we would expect it to either
117117
return unit (if the function modifies the set in place), or it returns a
118-
new set representing the result of adding the new element. So we're
118+
new set representing the result of adding the new element. We're
119119
looking for signatures that look something like `elt -> t -> unit` or
120120
`t -> elt -> unit` (since we don't know what order the two parameters
121121
should appear in), or `elt -> t -> t` or `t -> elt -> t`.
@@ -124,9 +124,9 @@ Skimming through the list, we see 2 functions with matching signatures:
124124
`add : elt -> t -> t` and `remove : elt -> t -> t`. Based on their names,
125125
`add` is probably the function we're looking for. `remove` probably removes
126126
an element from a set, and using our intuition again, it does seem like
127-
the type signature makes sense: To remove an element from a set, you need
127+
the type signature makes sense. To remove an element from a set, you need
128128
to tell it what set you want to perform the removal on and what element
129-
you want to remove; and the return result will be the resulting set after
129+
you want to remove. The return result will be the resulting set after
130130
the removal.
131131

132132
Furthermore, because we see that these functions return `t` and not `unit`,
@@ -163,16 +163,16 @@ comparison instead. To do this, we simply have to change the parameter
163163
that we pass to the `Set.Make` function.
164164

165165
The `Set.Make` function expects a struct with two fields: a type `t`
166-
that represents the type of the element, and a function `compare`
166+
that represents the type of the element and a function `compare`,
167167
whose signature is `t -> t -> int` and essentially returns 0 if two
168168
values are equal, and non-zero if they are non-equal. It just so happens
169169
that the `String` module matches that structure, which is why we could
170170
directly pass `String` as a parameter to `Set.Make`. Incidentally, many
171171
other modules also have that structure, including `Int` and `Float`,
172-
and so they too can be directly passed into `Set.Make` to construct a
173-
set of integers, or a set of floating point numbers.
172+
so they too can be directly passed into `Set.Make` to construct a
173+
set of integers or a set of floating point numbers.
174174

175-
For our use case, we still want our elements to be of type string, but
175+
For our use case, we still want our elements to be a string, but
176176
we want to change the comparison function to ignore the case of the
177177
strings. We can accomplish this by directly passing in a literal struct
178178
to the `Set.Make` function:
@@ -225,16 +225,16 @@ end);;
225225

226226
## Technical Details
227227

228-
### Set.Make, types and modules
228+
### `Set.Make`, Types, and Modules
229229

230230
As mentioned in a previous section, the `Set.Make` function accepts a structure
231-
with two specific fields, `t` and `compare`. Modules have structure, and thus
231+
with two specific fields, `t` and `compare`. Modules have structure, so
232232
it's possible (but not guaranteed) for a module to have the structure that
233-
`Set.Make` expects. On the other hand, types do not have structure, and so you
233+
`Set.Make` expects. On the other hand, types do not have structure, so you
234234
can never pass a type to the `Set.Make` function. In OCaml, modules start with
235-
an upper case letter and types start with a lower case letter. This is why
235+
an upper case letter, and types start with a lower case letter. So
236236
when creating a set of strings, you have to use `Set.Make(String)` (passing in
237-
the module named `String`), and not `Set.Make(string)` (which would be attempting
237+
the module named `String`) and not `Set.Make(string)` (which would be attempting
238238
to pass in the type named `string`, which will not work).
239239

240240
### Purely Functional Data Structures
@@ -247,7 +247,7 @@ that you create are immutable. The functions like `add` and `remove` do not
247247
actually modify the set you pass in, but instead return a new set representing
248248
the results of having performed the corresponding operation.
249249

250-
### Full API documentation
250+
### Full API Documentation
251251

252252
This tutorial focused on teaching how to quickly find a function that does what
253253
you want by looking at the type signature. This is often the quickest and most

0 commit comments

Comments
 (0)