Skip to content

Commit

Permalink
Fix broken sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Cuihtlauac ALVARADO committed Nov 27, 2023
1 parent fe72956 commit a80dc30
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions data/tutorials/language/0lg_10_polymorphic_variants.md
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ Let's consider this exception-raising code:
val f_exn : int -> int -> int = <fun>
```

The following is an attempt at translating `f_exn` with `result` values instead of exceptions. It is using the `Result.bind` instead of `|>`:
The following is an attempt at translating `f_exn` with `result` values instead of exceptions. It is using `Result.bind` as a [binding operator](docs/operators#binding-operators) instead of `|>`:
```ocaml
# type init_error = Negative_length;;
Expand All @@ -526,6 +526,10 @@ type nth_error = Too_short of int * int | Negative_index of int
| Failure _ -> Error (Too_short (List.length u, i))
val nth : 'a list -> int -> ('a, nth_error) result = <fun>
# let ( let* ) = Result.bind;;
val ( let* ) : ('a, 'b) result -> ('a -> ('c, 'b) result) -> ('c, 'b) result =
<fun>
# let f_res m n =
let* u = init m Fun.id in
let u = List.map (fun n -> n * n) u in
Expand All @@ -545,10 +549,6 @@ Binding can't change the `'e` type, while this code needs it to change throughou

Here is an equivalent version using polymorphic variants:
```ocaml
# let ( let* ) = Result.bind;;
val ( let* ) : ('a, 'b) result -> ('a -> ('c, 'b) result) -> ('c, 'b) result =
<fun>
# let init n f = try Ok (List.init n f) with
| Invalid_argument _ -> Error `Negative_length;;
val init : int -> (int -> 'a) -> ('a list, [> `Negative_length ]) result =
Expand Down

0 comments on commit a80dc30

Please sign in to comment.