A Look at ReasonML

Posted on September 10, 2018

I been interested in functional programming for awhile, but the trouble with functional programming is often times the easy things are hard, and it can be hard to identify good projects to work on. However, their is one functional language that has caught my interest, and it is ReasonML.

Reason, or ReasonML is a dialect of OCaml designed to work with JavaScript because of its relationship to BuckleScript, a OCaml to JavaScript compiler that is also used for Reason. The virtue of this then, is that Reason can not only use both JavaScript and OCaml libraries, it can function on many parts of the stack as well. Beyond this, the pragmatic approach Reason takes to functional programming, inherited from OCaml, is very useful.

A Comparison

In many imperative languages, one would do FizzBuzz like this:

While their is nothing wrong with using imperative style loops to solve questions like FizzBuzz. Sometimes, looping is not quite the tool need. ReasonML comes in with its functional background with a concept known as pattern matching. Pattern matching is the act of looking at a sequence of tokens/variables and only applying a method if the token fits the case. This can be seen below with this idea being applied to FizzBuzz:

To explain this function, FizzBuzz is a function that takes in a integer labeled x, and outputs a string as its result. Then below their is a switch statement, a way to signify that pattern matching is going on. After the statement, various cases are presented, so when x falls into a certain case, it then outputs the result it matches. However, we are getting ahead of ourselves. While this is a good example of the power of ReasonML, to get to this point, one needs to understand the basics of the language.

Some Pointers

Like in most languages, the hello world statement is rather simple:

The virtue of ReasonML/OCaml, is that due to its strong type system, their are several different print statements, each devoted to a different type of variable, from integers to strings.

Variables, like in many functional languages, are immutable. Here is an example of a variable along with basic concatenation:

The strength of the OCaml type system allows one not to declare types, but declaring types in variables is simple and can prove to be helpful, especially with functions.

To look at an example of declaring types, lets using the very common square function example:

If one decided to declare a as a string, the compiler would not let you do that. That proves to be a useful guiding hand when working with functions in ReasonML.

As seen, functions are declared also with a let statement, but conveniently, befitting a functional programming language, recursion gets treated uniquely when declaring functions. While not needed, a rec when declaring a ReasonML function shows to you and the compiler that you are declaring a recursive function. To give a simple example of a recursive function, here is fibonacci done in Reason:

While not in the scope in this tutorial, the reason we are able to do a more imperative style loop, as due to the interoperability of JavaScript and Reason, one can use JavaScript in Reason code. However, it is recommended to make sure you declare your types properly. This trait combined with ReasonMLs legacy of imperative style from OCaml, lets one do imperative programming when needed.

While their is a lot more I want to talk about with ReasonML, these are a few simple things that caught my eye, and I found interesting to compare in contrast. Their are more interesting topics to write about ReasonML however, and I will be covering them in the future. I believe that ReasonML represents a interesting development not only in how we do JavaScript, but how it expands OCaml to people who would not normally code in it. For just this, that makes Reason something to keep your eyes peeled on.