Skip to content

Commit 0afe607

Browse files
committed
(docs) comment out stray comment in Higher Order Functions tutorial
1 parent dc6da52 commit 0afe607

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

Diff for: data/tutorials/language/0it_04_higher_order_functions.md

+35-33
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ In OCaml, working with functions quickly becomes second nature. We like to think
4242

4343
Take for example a function that says hello to a person by name:
4444

45-
```ocaml
45+
```ocaml
4646
# let say_hi name = print_string ("Hello, " ^ name ^ "!\n") ;;
4747
val say_hi : string -> unit = <fun>
4848
```
4949

5050
We can call this function several times, to say "hello" to several people:
5151

52-
```ocaml
52+
```ocaml
5353
#say_hi "Xavier";;
5454
Hello, Xavier!
5555
- : unit = ()
@@ -65,7 +65,7 @@ Hello, Joe!
6565

6666
If we wanted to say "hello" to the same person multiple times, we'd just _repeat_ the same line of code.
6767

68-
```ocaml
68+
```ocaml
6969
# say_hi "Camel";;
7070
Hello, Camel!
7171
- : unit = ()
@@ -81,7 +81,7 @@ Hello, Camel!
8181

8282
One way we can avoid having to repeat these lines every time is by writing a function to say "hi" 3 times:
8383

84-
```ocaml
84+
```ocaml
8585
# let say_hi_3_times name =
8686
say_hi name;
8787
say_hi name;
@@ -100,7 +100,7 @@ When this happens, it usually means that the function is making certain decision
100100

101101
So instead, we will create a function that **let's the caller decide** how many times to say "hi." We do this by requiring a new argument, in this case, `times`:
102102

103-
```ocaml
103+
```ocaml
104104
# let rec say_many_hi times name =
105105
if times < 1 then ()
106106
else begin
@@ -136,11 +136,11 @@ Hello, Camel!
136136
- : unit = ()
137137
```
138138

139-
Unfortunately, reusing this _repetition_ behaviour isn't so easy because we have hard-coded our call to `say_hi`.
139+
Unfortunately, reusing this _repetition_ behaviour isn't so easy because we have hard-coded our call to `say_hi`.
140140

141141
To make this reusable, we can **let the caller decide** what our function should do:
142142

143-
```ocaml
143+
```ocaml
144144
# let rec repeat times thing_to_do =
145145
if times < 1 then ()
146146
else begin
@@ -153,7 +153,7 @@ val repeat : int -> 'a -> unit = <fun>
153153

154154
But what should `thing_to_do` be? Our intuition may be that we can call:
155155

156-
```ocaml
156+
```ocaml
157157
# repeat 3 (say_hi "Camel");;
158158
Hello, Camel!
159159
- : unit = ()
@@ -206,7 +206,7 @@ And we can use `repeat` to recreate our original `say_many_hi`, or to repeat any
206206
```ocaml
207207
# let say_many_hi times name = repeat times (fun () -> say_hi name);;
208208
val say_many_hi : int -> string -> unit = <fun>
209-
209+
210210
# let print_big_space () = repeat 10 print_newline;;
211211
val print_big_space : unit -> unit = <fun>
212212
```
@@ -233,13 +233,13 @@ module StringSet :
233233
|> StringSet.of_list
234234
|> StringSet.iter fn;;
235235
val only_once : (string -> unit) -> string list -> unit = <fun>
236-
236+
237237
# let yell_hi name =
238238
name
239239
|> String.uppercase_ascii
240240
|> say_hi;;
241241
val yell_hi : string -> unit = <fun>
242-
242+
243243
# let call_for_dinner names = only_once yell_hi names;;
244244
val call_for_dinner : string list -> unit = <fun>
245245
```
@@ -259,7 +259,7 @@ In the wild, there's certain patterns that repeat over and over again. It's usef
259259

260260
### Currying and Uncurrying
261261

262-
Since in OCaml all functions really just take one parameter, when you call `add x y`, you're actually calling two functions! `((add x) y)`
262+
Since in OCaml all functions really just take one parameter, when you call `add x y`, you're actually calling two functions! `((add x) y)`
263263

264264
Sometimes it helps to apply _parts_ of a function in different orders, and sometimes it helps to make a function really take all its parameters _at once_.
265265

@@ -280,7 +280,7 @@ The output function will have type `('a * 'b) -> 'c`. Notice how the arguments `
280280

281281
Here's our helper:
282282

283-
```ocaml
283+
```ocaml
284284
(* [uncurry] takes a function that is normally curried,
285285
and returns a function that takes all arguments at once. *)
286286
# let uncurry f (x, y) = f x y;;
@@ -393,14 +393,14 @@ Sometimes it makes sense to curry a function, and sometimes the clearer thing is
393393

394394
For example, this is a pipeline with a lot of currying/uncurrying that would most likely be easier to read and maintain if we manually wrote out the wrapper functions:
395395

396-
```ocaml
396+
```ocaml
397397
let do_and_return f x = f x; x
398398
;;
399399
400400
let flip (x, y) = (y, x)
401401
;;
402402
403-
names
403+
names
404404
|> List.map (do_and_return (greet "👋🏼"))
405405
|> List.map (Fun.flip List.assoc (List.map flip people))
406406
|> List.iter (curry reveal "The OCamler")
@@ -449,7 +449,7 @@ But this is not so easy read sometimes, especially as the number of functions gr
449449
To avoid this we have use the `|>` operator:
450450

451451
```ocaml
452-
let c = foo () |> bar |> baz in
452+
let c = foo () |> bar |> baz in
453453
(* ... *)
454454
```
455455

@@ -462,7 +462,7 @@ let (|>) x fn = fn x
462462

463463
It receives a value `x` and a function `fn` and as soon as it has both, it calls `fn` with `x`. This lets us invert the order and build pipelines that read left-to-right or top-to-bottom instead of inside-out.
464464

465-
But what happens when our functions have more than one argument?
465+
But what happens when our functions have more than one argument?
466466

467467
Let's look at an example of string manipulation. We want to get the domain name from an email.
468468

@@ -500,9 +500,11 @@ email
500500
;;
501501
```
502502

503-
(** NOTE(@leostera): this example kinda sucks, i'd like one where the use of labels greatly improves the readability but since `ListLabels.nth_opt` doesn't take an argument then we still need that nasty fun flip :( will get back t othis)
503+
<!--
504+
NOTE(@leostera): this example kinda sucks, i'd like one where the use of labels greatly improves the readability but since `ListLabels.nth_opt` doesn't take an argument then we still need that nasty fun flip :( will get back t othis)
505+
-->
506+
504507

505-
506508
### Iterating
507509

508510
We usually think of iteration when we think of looping, and going through collections of things:
@@ -516,7 +518,7 @@ Iterating in OCaml means that if there is a value (or more), we'd like to apply
516518

517519
#### Iterating over Lists
518520

519-
A list in OCaml is a linked-list that is composed by a head (the first element) and a tail (the rest of the list).
521+
A list in OCaml is a linked-list that is composed by a head (the first element) and a tail (the rest of the list).
520522

521523
We can iterate over lists by pattern matching on then. When doing so, we either get an empty list (`[]`), or we get a pattern with a head and a tail (`n :: rest`). On the branch with a head and a tail, we can directly use the head value and apply a function to it, and then recurse with the tail.
522524

@@ -560,7 +562,7 @@ let run_if_some opt fn =
560562
| Some value -> fn value
561563
| None -> ()
562564
;;
563-
565+
564566
let run_if_ok res fn =
565567
match res with
566568
| Ok value -> fn value
@@ -580,10 +582,10 @@ With either of those functions, we can put together an iterator over maps or set
580582

581583
```ocaml
582584
let iter values collection fn =
583-
let values : 'a list = values collection in
585+
let values : 'a list = values collection in
584586
List.iter fn values
585587
;;
586-
588+
587589
module StringSet = Set.Make(String);;
588590
module IntMap = Map.Make(Int);;
589591
@@ -604,10 +606,10 @@ But some data is _lazy_, and it only lets us access one element at a time. So if
604606
Lazy sequences in OCaml are represented with the `Seq` module, which has a function called `uncons` to get the next element. This function also returns the new sequence that we can use to get the 2nd element, and so on.
605607

606608
```ocaml
607-
let rec iter seq fn =
609+
let rec iter seq fn =
608610
match Seq.uncons seq with
609611
| None -> ()
610-
| Some (value, seq2) ->
612+
| Some (value, seq2) ->
611613
fn value;
612614
iter seq2 fn
613615
;;
@@ -625,7 +627,7 @@ We'll define our tree type to include 2 constructors. One for a leaf node, which
625627

626628
```ocaml
627629
type 'value tree =
628-
| Leaf of 'value
630+
| Leaf of 'value
629631
| Node of 'value tree * 'value
630632
;;
631633
```
@@ -648,10 +650,10 @@ Now before we define our iteration function, its important to define what iterat
648650
For our example, we'll iterate from the top down as we go along:
649651

650652
```ocaml
651-
let rec iter tree fn =
653+
let rec iter tree fn =
652654
match tree with
653655
| Leaf value -> fn value
654-
| Node (tree2, value) ->
656+
| Node (tree2, value) ->
655657
fn value;
656658
iter tree2 fn
657659
;;
@@ -749,7 +751,7 @@ If we wanted to implement a sum over the custom tree type we saw in the Iteratin
749751

750752
```ocaml
751753
type 'value tree =
752-
| Leaf of 'value
754+
| Leaf of 'value
753755
| Node of 'value tree * 'value
754756
;;
755757
@@ -772,7 +774,7 @@ let rec fold_tree tree fn =
772774

773775
But we quickly run into a problem: our `fn` function is meant to combine two items, so in the `Leaf` branch, what is the second item?
774776

775-
Folding requires us to define a _zero value_, a starting point for an accumulator, that will be used when the collection or data type is "empty".
777+
Folding requires us to define a _zero value_, a starting point for an accumulator, that will be used when the collection or data type is "empty".
776778

777779
Some data types don't have a good "empty" value. Our tree for example does not. Lists do have an empty list. Options have a `None` constructor. Results' don't have a good "empty" value either.
778780

@@ -787,7 +789,7 @@ let rec fold_tree tree fn acc =
787789
```
788790

789791
And voila! Our function now types correctly and we can use it to reduce our trees down to any value.
790-
792+
791793
### Sorting
792794

793795
Another common behavior usually implemented with higher-order functions is sorting collections.
@@ -955,7 +957,7 @@ type ('input, 'output) operations = {
955957
<!--
956958
Comment
957959
958-
let f () = 42
960+
let f () = 42
959961
let f () = g 42
960962
961963
One possible issue with the repeat function used in the intro comes from the fact
@@ -970,7 +972,7 @@ In CS3110, `twice` and `repeat` are pure functions
970972
```ocaml
971973
let twice f x = f (f x)
972974
973-
let rec repeat f n x = if n = 0 then x else repeat f (n - 1) (f x)
975+
let rec repeat f n x = if n = 0 then x else repeat f (n - 1) (f x)
974976
```
975977
976978
Trouble with those function is they aren't very realistic, but they are easier to understand (I believe).

0 commit comments

Comments
 (0)