# OCaml by Example?

**URL:** <https://discuss.ocaml.org/t/ocaml-by-example/4032>\
**Category:** Learning\
**Created:** [July 4, 2019, 6:27am UTC](https://discuss.ocaml.org/t/ocaml-by-example/4032 "2019-07-04T06:27:49Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [July 4, 2019, 6:27am UTC](https://discuss.ocaml.org/t/ocaml-by-example/4032/1 "2019-07-04T06:27:49Z")

</div>

Is there an “OCaml by Example” book/tutorial somewhere?

I’m familiar with Rust/Clojure and want to learn OCaml syntax by typing lots of OCaml examples, trying to run the code, and fixing any typos.

---

<div class="post-metadata">

**Author:** ![vrotaru](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/vrotaru/32/39_2.png) [@vrotaru](https://discuss.ocaml.org/u/vrotaru)\
**Post date:** [July 4, 2019, 12:47pm UTC](https://discuss.ocaml.org/t/ocaml-by-example/4032/2 "2019-07-04T12:47:05Z")

</div>

See [99 (solved) problems](https://ocaml.org/learn/tutorials/99problems.html). The official OCaml manual has also a lot of examples,

---

<div class="post-metadata">

**Author:** ![fccm](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/fccm/32/1587_2.png) [@fccm](https://discuss.ocaml.org/u/fccm)\
**Post date:** [July 5, 2019, 5:59am UTC](https://discuss.ocaml.org/t/ocaml-by-example/4032/3 "2019-07-05T05:59:03Z")

</div>

There are a LOT of examples on RosettaCode:  
[http://rosettacode.org/wiki/Category:OCaml](http://rosettacode.org/wiki/Category:OCaml)

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [July 5, 2019, 6:38am UTC](https://discuss.ocaml.org/t/ocaml-by-example/4032/4 "2019-07-05T06:38:56Z")

</div>

My original question was not well written.

I’m looking for the OCaml equiv of [https://doc.rust-lang.org/rust-by-example/](https://doc.rust-lang.org/rust-by-example/)

something that intends to explain OCaml syntax / features one by one, by examples.

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [July 5, 2019, 6:42am UTC](https://discuss.ocaml.org/t/ocaml-by-example/4032/5 "2019-07-05T06:42:36Z")

</div>

Maybe you’re looking for Yaron Minsky’s _Real World Ocaml_ book?

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [July 5, 2019, 6:45am UTC](https://discuss.ocaml.org/t/ocaml-by-example/4032/6 "2019-07-05T06:45:21Z")

</div>

A bit OT, but please be careful with RosettaCode. It contains [plain wrong](https://rosettacode.org/wiki/Return_multiple_values#Space_safety_of_tuples) information about OCaml which hasn’t been corrected for years.

If anyone on this forum has an account there please correct it, there’s a discussion about this [here](https://stackoverflow.com/questions/46985668/ocaml-product-type-with-independent-reachability-for-each-element) (see my answer).

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [July 5, 2019, 6:59am UTC](https://discuss.ocaml.org/t/ocaml-by-example/4032/7 "2019-07-05T06:59:39Z")

</div>

> [@zeroexcuses](#):
>
> something that intends to explain OCaml syntax / features one by one, by examples.

Having a cursory look at the rust book you have provided I think the closest you can get to this is John Whitington’s series of books starting with [OCaml from the Very Beginning](http://ocaml-book.com/).

His books are the best ones I ever read about OCaml.

---

<div class="post-metadata">

**Author:** ![fccm](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/fccm/32/1587_2.png) [@fccm](https://discuss.ocaml.org/u/fccm)\
**Post date:** [July 21, 2019, 5:59am UTC](https://discuss.ocaml.org/t/ocaml-by-example/4032/8 "2019-07-21T05:59:03Z")

</div>

> [@dbuenzli](#):
>
> A bit OT, but please be careful with RosettaCode. It contains [plain wrong](https://rosettacode.org/wiki/Return_multiple_values#Space_safety_of_tuples) information about OCaml which hasn’t been corrected for years.

Could you explain what is wrong there, we still get the same result with a recent version of OCaml.

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [July 21, 2019, 8:24am UTC](https://discuss.ocaml.org/t/ocaml-by-example/4032/9 "2019-07-21T08:24:28Z")

</div>

My answer on stackoverflow explains what is wrong. I won’t repeat it here.

---

<div class="post-metadata">

**Author:** ![fccm](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/fccm/32/1587_2.png) [@fccm](https://discuss.ocaml.org/u/fccm)\
**Post date:** [July 21, 2019, 10:02am UTC](https://discuss.ocaml.org/t/ocaml-by-example/4032/10 "2019-07-21T10:02:13Z")

</div>

> [@dbuenzli](#):
>
> My answer on stackoverflow explains what is wrong. I won’t repeat it here.

Sorry but I don’t understand your post on stackoverflow, I don’t find your explanation very clear, so maybe someone else could explain it here?

Also I’m not sure to understand is it really “plain wrong” as in “it was never true”, or do you mean it’s just “out of date”? Because there is this explanation from the ocaml mailing list:

[http://caml.inria.fr/pub/ml-archives/caml-list/2008/05/9fbbd2d5896fb5a78236e2ebbe2c4c4a.en.html](http://caml.inria.fr/pub/ml-archives/caml-list/2008/05/9fbbd2d5896fb5a78236e2ebbe2c4c4a.en.html)

> [@Yaron Minsky](#):
>
> > - In tuple.ml, I find this worrying comment:
> > 
> > I didn’t expect that, and that’s quite annoying! Shouldn’t that be fixed  
> > in the Ocaml compiler?
> 
> We would prefer that too, but we haven’t been able to convince anyone that  
> it’s worth doing. There are apparently some reasons that it’s painful to  
> fix in the bytecode compiler, and there’s some reluctance to make the GC  
> behavior be significantly different between native and bytecodes.

So do you mean this was fixed already, and if so since which version of OCaml?

---

<div class="post-metadata">

**Author:** ![dmbaturin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dmbaturin/32/2074_2.png) [@dmbaturin](https://discuss.ocaml.org/u/dmbaturin)\
**Post date:** [July 21, 2019, 11:48am UTC](https://discuss.ocaml.org/t/ocaml-by-example/4032/11 "2019-07-21T11:48:44Z")

</div>

I’ve been meaning to make one for a while. Past year I wrote the first chapters as a blog post series, but then life happened™. Until life happens again in September, perhaps I can make a few more.

I’ve made an initial import to git ([https://github.com/dmbaturin/ocaml-book/](https://github.com/dmbaturin/ocaml-book/)) and uploaded a rough, nearly unstyled build to [http://ocaml-book.baturin.org/](http://ocaml-book.baturin.org/)

What sets it apart from other books is that it’s under a free license (CC-BY-SA) and belongs to the community. I hope it can become a collaborative effort.

The examples are meant to be compile-ready (not being REPL-centric is one of the goals), so I think it should be easy enough to make them runnable.

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [July 21, 2019, 12:55pm UTC](https://discuss.ocaml.org/t/ocaml-by-example/4032/12 "2019-07-21T12:55:59Z")

</div>

> [@fccm](#):
>
> Sorry but I don’t understand your post on stackoverflow, I don’t find your explanation very clear, so maybe someone else could explain it here?

Maybe the explanation is not clear but if would have to write it again I would write exactly the same, sorry. It’s difficult to help if you just say “I don’t find it clear”.

In any case if you understand what rosetta says you actually get in my post an [executable proof](https://stackoverflow.com/a/46988081/442602) that what is written on rosetta, namely:

> The OCaml programmer should be aware that when multiple values are returned with a tuple, the finalisation does not handle each values independently, but handles the tuple as a whole. So all the values are only finalised when all the values are not reachable anymore.

is wrong in general and moreover (if you understand the rest of the answer) is so _most_ of the time.

I think it’s very bad to have that kind of wrong information on a site a newcomer is likely to consult and the reason why this bit should be removed from that website altogether. It also makes the language look completely broken from a memory management perpsective which we all know on this forum is clearly not.

---

<div class="post-metadata">

**Author:** ![dmbaturin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dmbaturin/32/2074_2.png) [@dmbaturin](https://discuss.ocaml.org/u/dmbaturin)\
**Post date:** [July 21, 2019, 1:01pm UTC](https://discuss.ocaml.org/t/ocaml-by-example/4032/13 "2019-07-21T13:01:44Z")

</div>

Was it ever true, or it has been wrong all along?

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [July 21, 2019, 1:08pm UTC](https://discuss.ocaml.org/t/ocaml-by-example/4032/14 "2019-07-21T13:08:20Z")

</div>

To be honest, I don’t know but I highly suspect that what I wrote in the answer has always been true (cf. also the “worrying comment” above that mentions pattern _matching_, not tuples in general).

---

<div class="post-metadata">

**Author:** ![fccm](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/fccm/32/1587_2.png) [@fccm](https://discuss.ocaml.org/u/fccm)\
**Post date:** [July 21, 2019, 5:26pm UTC](https://discuss.ocaml.org/t/ocaml-by-example/4032/15 "2019-07-21T17:26:26Z")

</div>

It doesn’t mentions pattern matching but functions returning tuples (at least as long as what I can understand).

[found this](https://github.com/janestreet/core/search?q=Space_safe_tuple), so it seems that this problem was fixed with OCaml version 4.00.0

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [July 21, 2019, 6:41pm UTC](https://discuss.ocaml.org/t/ocaml-by-example/4032/16 "2019-07-21T18:41:39Z")

</div>

> [@fccm](#):
>
> It doesn’t mentions pattern matching but functions returning tuples

That’s precisely what this rosetta comment gets completely wrong. I don’t think the following pattern for any function `f` returning a tuple:

```auto
let x = fst (f r)

```

ever had a space leak in any of the ocaml versions. Yet that’s what it will lead you to believe.

---

<div class="post-metadata">

**Author:** ![fccm](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/fccm/32/1587_2.png) [@fccm](https://discuss.ocaml.org/u/fccm)\
**Post date:** [July 21, 2019, 7:07pm UTC](https://discuss.ocaml.org/t/ocaml-by-example/4032/17 "2019-07-21T19:07:47Z")

</div>

```auto
git clone https://github.com/janestreet/core
cd core/
git checkout 9b123844eb5ba519be647
cd lib/
cat space_safe_tuple.mli
(** The raison d'etre for Space_safe_tuple<N> is that OCaml doesn't properly
    free variables matched in tuple patterns. If one writes

      let (a, b) = ... in
      ... a ... b ...

    when a and b appear at most once in the subsequent expression
    then this effectively becomes

      let t = ... in
      ... (fst t) ... (snd t) ...

    Hence, references to [a] and [b] keep alive the entire tuple. This can
    lead to surprising space leaks.

    One notable instance of this bad behaviour is when one writes:

    let _,a = ... in ...a...

    The first element of the tuple, albeit not even being named, is live in the
    subsequent expression.

    By using Space_safe_tuple<N>, one makes it clear to the user where the tuple
    selection happens, and (hopefully) causes them to think about space safety,
    and to write the following if they want to free the tuple.

      let t = ... in
      let a = Space_safe_tuple2.get1 t in
      let b = Space_safe_tuple2.get2 t in
      ... a ... b ...
*)
module T2 : sig
  type ('a, 'b) t
  val create : 'a -> 'b -> ('a, 'b) t
  external get1 : ('a, _) t -> 'a = "%field0"
  external get2 : (_, 'a) t -> 'a = "%field1"
end

module T3 : sig
  type ('a, 'b, 'c) t
  val create : 'a -> 'b -> 'c -> ('a, 'b, 'c) t
  external get1 : ('a, _, _) t -> 'a = "%field0"
  external get2 : (_, 'a, _) t -> 'a = "%field1"
  val get3 : (_, _, 'a) t -> 'a
end

```

For what I can understand, it actually talks about a memory leaks for functions returning a tuple, and it proposes to use equivalents of fst and snd as a fix.

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [July 21, 2019, 7:11pm UTC](https://discuss.ocaml.org/t/ocaml-by-example/4032/18 "2019-07-21T19:11:43Z")

</div>

> [@fccm](#):
>
> For what I can understand, it actually talks about a memory leaks for functions returning a tuple, and it propose to use equivalents of fst and snd as a fix.

Read carefully what you are quoting. It’s not the fact that you are _returning_ tuples from functions that is problematic it’s the fact that you _pattern match_ on them.

---

<div class="post-metadata">

**Author:** ![fccm](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/fccm/32/1587_2.png) [@fccm](https://discuss.ocaml.org/u/fccm)\
**Post date:** [July 21, 2019, 7:13pm UTC](https://discuss.ocaml.org/t/ocaml-by-example/4032/19 "2019-07-21T19:13:22Z")

</div>

I’m sorry but I don’t catch.

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [July 21, 2019, 7:15pm UTC](https://discuss.ocaml.org/t/ocaml-by-example/4032/20 "2019-07-21T19:15:39Z")

</div>

This won’t leak the second component:

```ocaml
let x = fst (f r)

```

This might leak the second component _in the scope of the `let` binding_, i.e. until `e` finished executing:

```ocaml
let x, _ = f r in
e

```

[Next page](https://discuss.ocaml.org/t/ocaml-by-example/4032.md?page=2)
