Invitation to a new Ocaml tutorial - Imperative, Logic, Javascript

Hello,
Imperative. Think Python is a renowned resource for absolute programming beginners starting with Python. It got translated to Think Julia alongside other languages.

  1. Suggestion. Why don’t we develop our port? It would follow the same structure of imperative languages, but introduces the functional counter-part.

The book would have a chapter in looping for example but maybe without any loops. Even if graph traversal is not trivial in the functional paradigm, it gets mentioned early, as mutable graph traversal shows early in imperative programming.

Logic. Introduction to Computation: Haskell, Logic and Automata is a supremely beautiful book that introduces computation through the lens of discrete math and logic, alongside Haskell implementations.

  1. Suggestion. Why don’t we summarize its chapters and translate Haskell snippets to Ocaml? This could be a very nice supplementary to Discrete Math courses and Math major students. We will ask for a permission from the authors of course.

For example recursion is introduced within the context of proofs by induction.

Javascript. The motivation is somehow similar to Melange for Javascript Developers.

  1. Suggestion. Javascript developers deserve a better resource.

Conclusion. The idea is to introduce ocaml through the comfort zone of imperative programmers, mathematicians, and javascript developers.

Discussion. What’s your feedback? Any advise or recommendation?

1 Like

P.S. Are you aware of SICP and its Javascript and Python ports?

Most books about a given language doesn’t take as an precondition the knowledge of such or such language. We already have multiple of OCaml books.

Sure we can have dedicated books like Ada for the C++ or Java Developer — learn.adacore.com (sorry, C++ or Java to Ada). But are they very useful here ? I mean transliterate a procedural algorithm into OCaml would be quite clumsy. The whole algorithm should be designed at first as a functional one is possible.

About JavaScript… I guess that many JS programmers don’t use it as a functional language. Then a book about the conversion of some syntaxes to an other won’t necessarily be useful. However, a tutorial « how to program in OCaml, translate it in JS (automatically with dune), how to have a FFI OCaml/JS » should be interesting if not already written.

I don’t understand why a graph traversal can be difficult in functional paradigm. A tree is ok, but ok, a general graph could be more efficient with a mutable seen variable (not pure functional, but OCaml would be happy with it).

books about a given language doesn’t take as an precondition the knowledge of such or such language.

I don’t want to introduce OCaml language considering a background in javascript, Python, C++, or any language knowledge. I want to introduce how the functional paradigm can be utilized for the problems developers commonly face.

I mean transliterate a procedural algorithm into OCaml would be quite clumsy. The whole algorithm should be designed at first as a functional one is possible.

I agree. What I am suggesting is to start with the problems an imperative programmer commonly faces, then solve them using the functional paradigm genuine methods and techniques.

a book about the conversion of some syntaxes to an other won’t necessarily be useful.

I agree.

many JS programmers don’t use it as a functional language.

Exactly. That’s why showing the functional paradigm using Typescript or Civet is going to be useful for them, then OCaml shines!

how to program in OCaml, translate it in JS (automatically with dune), how to have a FFI OCaml/JS

I agree. That’s a killer feature of OCaml, yet no accessible tutorial is available.

I don’t understand why a graph traversal can be difficult in functional paradigm

Software engineers usually care about quick prototyping and meeting deadlines more than elegance, correctness, and efficiency. The higher the entry barrier of functional programming, the less useful it is for engineers. OCaml needs more concrete demonstrations like Free Code Camp and Design Patterns in Typescript. If low-level doc is the only way to do a small project, then OCaml won’t attract outsiders.

But here Learn OCaml we have several books about OCaml… after reading them, perhaps we miss some skills about extra OCaml stuff (opam, dune).

I also miss some practical hints… (ok from Windows, what should I do?)

And I think no book would spread the whole coverage of the whole Opam repository. We have adapt.

we have several books about OCaml

Example 1. It took me 10+ minutes to figure the logical AND operator is && through this doc page. I discovered later it is in John’s book here. It is not in Your First OCaml Program.

Example 2. Printing a recursive type is a basic simple task any newcomer developer shall face. I cannot find a single accessible tutorial or cookbook that mentions [@@deriving show { with_path = false }]

type tree =
    | Leaf of float
    | Node of float * tree * tree
    [@@deriving show { with_path = false }]

The community should never expect a developer to go through low-level doc and books, and invest time, only to perform a simple task.

We have resources about OCaml and functional programming, but we don’t have resources for nowadays software engineers. People learn what is valuable to fulfill their needs.

Example 1: the introduction page has not the aim of teaching you how to program in OCaml, but only the first step (setting up dune) before starting a project.

Example 2: one book about OCaml is Real World OCaml and introduces some ppx writers. There are many of them (see GitHub - ocaml-community/awesome-ocaml: A curated collection of awesome OCaml tools, frameworks, libraries and articles.) and they are not part of the language but are extensions.

Your remark makes me think of LaTeX. It has an official book by the author of the first edition revised to adapt to the 2e edition. The revision doesn’t cover every aspect… there is also several other books: « the LaTeX Companion » is a famous one: it presents many many extension then we can select the right extension for our need a start using it. But the book hasn’t the aim of replacing the documentation of each extension.

I’d just like to offer a general encouragement and support for more learning materials. There are countless different ways to learn a subject, and each new perspective, author, approach, etc. brings something new to the table. I cannot see a downside of more good quality learning material. All the efforts you mention seem like they could be useful to certain audiences.

IIUC, Category theory for Programmers book - OCaml flavor is in the spirit of your suggestions.

2 Likes

they are not part of the language but are extensions.

Exactly. Because they are extensions, they did not get introduced early.

it presents many many extension then we can select the right extension for our need a start using it. But the book hasn’t the aim of replacing the documentation of each extension.

I like the analogy of LaTeX you had drawn, and I believe we need the equivalent of The LaTeX Companion for functional programming.

I’d just like to offer a general encouragement and support for more learning materials

I am happy to volunteer but it is worth to mention I am a beginner in OCaml. I need expert reviewers to produce a quality publishable content.

1 Like

Someone had posted the link An Introduction to js_of_ocaml - HackMD (see It there a tutorial for `js_of_ocaml` with simple graphics? - #7 by Phage)

I guess that it should be added to the Ocaml.org site as a useful ressource.

1 Like

I posted the link, I had nothing to do with that tutorial BTW, but KUDOs to whoever wrote it.

I loved that tutorial so much I wrote my own variant of it doing everything from scratch again but a little differently. Not sure my ‘tutorial’ is any good really… it’s just me writing down some things as ‘struggle’ through the process of creating simple ‘counter’ webapp. I’m almost embarassed to share my possibly clueless ramblings with this community of experts.

Anyway here is my version of the Js_of_ocaml tutorial.

I will echo, and you’ll see it in the tutorial ramblings, what some others have suggested here also:

While Ocaml is a hard language to learn… that’s not my biggest hurdle. What I often struggle with a lot more are some rather ‘mundane’ things such as:

  • how to create a project with dune and opam
  • how to add test cases and run them
  • how to add dependencies
  • how to setup and IDE so it works well, how to debug-step your code,
  • how to print values on the console
  • why is dune complaining about some cryptic thing I don’t even understand…
  • etc.

Tutorials about that sort of thing would be very welcome.

2 Likes