markdown guide
 

ReasonML is a functional, eager, impure, safely typed language which bridges OCaml and JavaScript. It is a general-purpose language which can compile to native code or JS.

To better appreciate what the above means, I'd like to give a super-condensed history lesson.

The Tiniest History of FP

In the 1930s, mathematicians were trying to figure out what it meant to compute something. Alonzo Church invented a kind of symbol replacement language called the Lambda Calculus; Alan Turing invented a hypothetical symbol re-writing machine called the Turing Machine.

Turing Machines are cool because if a hypothetical machine can calculate anything, why not just make a real one? Hence, actual computers (and many languages written to command them) are heavily based on this concept of a stateful machine, with memory that changes over time. Examples of these imperative languages include machine code, assembly, C, and (to some extent) Ruby / Python / JS.

Astonishingly, though, it turns out Turing Machines and the Lambda Calculus are the exact same thing just expressed differently. Lambda Calculus is cool because as a language it has zero concept of information changing over time; it is a "pure" functional language in which the only entity is functions that take in inputs and return outputs.

From this concept, people have made many functional languages which avoid the pitfalls of state in various clever ways. There was LISP in 1958, which influenced ML in 1973, which in turn was realized as the dialect OCaml in 1996. (There are many other interesting functional languages such as Haskell and Elm, but since ReasonML is a dialect of OCaml we will not cover them).

But Why FP?

So this idea of a functional language has actually been worked on and developed for many decades, and the languages involved are pretty mature. People like working in functional languages because:

  • functions are extremely composable (they combine together well), making it easier to build a complex program out of small parts
  • pure functions are easier to reason about, preventing "magic", unexpected race conditions, errors in state management etc.
  • the ideas underpinning functional programming are law-based and universal, making them translate between different languages in a seamless way
  • the restrictions of avoiding state have given rise to some very useful and brilliant abstractions

In particular, the ML family of languages and languages influenced by them (e.g. Haskell) benefit from a remarkable static type system which not only protects the developer from a whole class of errors, but which can be inferred (figured out) by the compiler without forcing the developer to declare the type of every single value (à la C).

So What is ReasonML?

Modern web dev orbits around JS, which is what browsers can run. JS as a language has both good and not-so-good aspects, and some people would like to code in something more safe (in the sense of it prevents possible errors) and/or clever. Thus, many people write their applications in different languages, which compile / transpile to JS – for example CoffeeScript (not so much these days), TypeScript, Elm, and PureScript.

The OCaml community has already been able to compile OCaml programs to JS for a while now, with the help of Bucklescript – which is not a language (despite the name) but rather a compiler backend. So the tech needed to convert OCaml to JS is pretty mature.

However, asking web developers who get their start in JS to learn OCaml is a big sell, partly because the syntax is dramatically different. So Facebook came along and designed a way of writing OCaml which has the same semantics (meaning), but different syntax (appearance). This alternative syntax is called (drumroll) ReasonML.

ReasonML is kind of like a language, but it's really just a new way of writing OCaml and have it easily turn into vanilla JavaScript. That's an implementation detail, and if you like you can think of it simply as a language for writing general-purpose programs and/or web apps, which gives you the feature benefits of a functional language but the familiarity of a JS-like syntax.

Summary

In short, ReasonML is:

  • a new syntax and set of tools for writing OCaml
  • a language which compiles to native code or JS
  • a functional language with various FP benefits

I am personally very bullish on ReasonML. Here's a very subjective comparison chart vis-à-vis web programming:

Language Functional? Easy for JS Devs? General-Purpose? Good JS compilation?
JavaScript ★★★★★ ★★★★ (with Node) ★★★★★
Haskell ★★★★★ ★★★★★
PureScript ★★★★★ ★★★ (JS only) ★★★★
Elm ★★★★ ★★★ ★ (frontend only) ★★★
OCaml ★★★ ★★★★★ ★★★★
ReasonML ★★★ ★★★★ ★★★★ (native) ★★★★

To me ReasonML hits a great sweet spot of benefitting from functional ideas, being easy to get up & running in, compiling well to JS, and being generally useful. The technologies it is based on (OCaml and Bucklescript) are fairly mature, although ReasonML itself is quite new and rapidly evolving. I'm interested to see how it pans out in the long term.

Hope this helps!

 

Great answer! I've been playing with ReasonML on weekends and it's been fun, though its still really young and rather difficult to find resources to learn from. I'm curious how your experience has been with it?

 

Thanks! However, I've been focusing mainly on Haskell for myself, so I cannot really go any deeper regarding ReasonML at the moment. Like I said, I'm bullish on it, but given my own (still learning, but nonzero) knowledge of pure FP, I probably would sooner focus on Elm and/or PureScript over ReasonML for my own front-end projects at this point, and Haskell for anything else. That may seem a little hypocritical, but one of the primary reasons I cited for my optimism re: ReasonML is the relatively low barrier to entry for people not already used to ML/Miranda-influenced syntax. Since I don't fall into that demographic anymore, my answer is admittedly a bit extrapolative / prescriptive. And in any case I'm not actively learning any of the above except Haskell as I said. Sorry I don't have much more insight to offer! I'm excited to see where the language goes though, and intend to eventually learn a lot more about it. :-)

EDIT: PS, I did find egghead.io/courses/get-started-wit... to be a nice beginner course, for anyone who stumbles across this. Just a starter, nothing thorough, but helpful nonetheless.

Classic DEV Post from Oct 14 '18

Basic Functional Programming Patterns in JavaScript

Using functional programming patterns for iteration

Aswath KNM profile image