Skip to content

Commit 5305db6

Browse files
author
Cuihtlauac ALVARADO
committed
Some more edits
1 parent cca2dde commit 5305db6

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

data/tutorials/ds_05_seq.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ type 'a list =
3636
| (::) of 'a * 'a list
3737
```
3838
and `Seq.t`, which is merely a type alias for `unit -> 'a Seq.node`. The whole
39-
point of this definition is the second argument's type `Seq.Cons`, which is a
40-
function returning a sequence while its `list` counterpart returns a list. Let's
39+
point of this definition is `Seq.Cons` second argument's type, which is a
40+
function returning a sequence while its `list` counterpart is a list. Let's
4141
compare the constructors of `list` and `Seq.node`:
4242
1. Empty lists and sequences are defined the same way, a constructor without any
4343
parameter: `Seq.Nil` and `[]`.
@@ -65,7 +65,7 @@ values are thunks. With the analogy used earlier, `a` is frozen in its thunk.
6565

6666
Here is how to build seemingly infinite sequences of integers:
6767
```ocaml
68-
# let rec ints n : int Seq.t = fun () -> Seq.Cons (n, ints (n + 1))
68+
# let rec ints n : int Seq.t = fun () -> Seq.Cons (n, ints (n + 1));;
6969
val ints : int -> int Seq.t = <fun>
7070
```
7171
The function `ints n` looks as if building the infinite sequence `(n; n + 1; n +
@@ -100,15 +100,15 @@ let rec take n seq () = match seq () with
100100
```
101101
`take n seq` returns, at most, the `n` first elements of the sequence `seq`. If
102102
`seq` contains less than `n` elements, an identical sequence is returned. In
103-
particular, if `seq` is empty, an empty sequence is returned.
103+
particular, if `seq` is empty, or `n` is negative, an empty sequence is returned.
104104

105105
Observe the first line of `take`. It is the common pattern for recursive
106106
functions over sequences. The last two parameters are:
107107
* a sequence called `seq`
108108
* a `unit` value
109109

110110
When executed, the function begins by unfreezing `seq` (that is, calling `seq
111-
()`) and then pattern matching to look inside the available data. However, this
111+
()`) and then pattern matching to look inside the data made available. However, this
112112
does not happen unless a `unit` parameter is passed to `take`. Writing `take 10
113113
seq` does not compute anything. It is a partial application and returns a
114114
function needing a `unit` to produce a result.
@@ -127,7 +127,7 @@ The `Seq` module also has a function `Seq.filter`:
127127
# Seq.filter;;
128128
- : ('a -> bool) -> 'a Seq.t -> 'a Seq.t = <fun>
129129
```
130-
It builds a sequence of elements satisfying a condition.
130+
It keeps elements of a sequence which satisfies the provided condition.
131131

132132
Using `Seq.filter`, it is possible to make a straightforward implementation of
133133
the [Sieve of
@@ -187,6 +187,7 @@ sequences. For instance, `Seq.ints` can be implemented using `Seq.unfold` in a
187187
fairly compact way:
188188
```ocaml
189189
let ints = Seq.unfold (fun n -> Some (n, n + 1));;
190+
val ints : int -> int Seq.t = <fun>
190191
```
191192

192193
As a fun fact, one should observe `map` over sequences can be implemented using
@@ -235,7 +236,7 @@ It actually isn't. It's a non-ending recursion which blows away the stack.
235236
# fibs 0 1;;
236237
Stack overflow during evaluation (looping recursion?).
237238
```
238-
This definition is behaving as expected:
239+
This definition is behaving as expected (spot the differences, there are four):
239240
```ocaml
240241
# let rec fibs m n () = Seq.Cons (m, fibs n (n + m));;
241242
val fibs : int -> int -> int Seq.t = <fun>
@@ -289,7 +290,7 @@ some of those functions:
289290
val String.to_seq : char Seq.t -> string
290291
```
291292
Similar functions are also provided for sets, maps, hash tables (`Hashtbl`) and
292-
others (except `Seq`, obviously). When implementing a datatype module, it is
293+
others (except `Seq`, obviously). When implementing a collection datatype module, it is
293294
advised to expose `to_seq` and `of_seq` functions.
294295

295296
## Miscellaneous Considerations
@@ -308,6 +309,6 @@ OCaml 4.14. Beware books and documentation written before may still mention it.
308309

309310
## Exercises
310311

311-
* [Diagonal](/problems#100)
312-
* [Streams](/problems#101)
312+
* [Streams](/problems#100)
313+
* [Diagonal](/problems#101)
313314

0 commit comments

Comments
 (0)