Steps to learning Reason OCaml

Myer Nore
Dec 10, 2017 - 7 min read

Reason Lists

Intro to Lists

A list in Reason OCaml is a linked list. A List is divided into a single-element head and the rest of the list, called the tail. Whenever you need random access to element of a collection, an Array or a Record may be a better structure. A list is a great data container other times, though, in line with the Rule of Least Power.

The Reason docs for lists are here, and the API docs for the List module are here.

List Pattern Matching Examples


The standard library has a List.length function, but if it didn't already, here is one possible implementation that illustrates pattern matching on Lists:

/* loading */

This shows how pattern matching can select empty lists [] as well as lists in the [head, ...tail] syntax. In Reason the ellipsis ... is meant to remind JS programmers of Rest parameters, as in "get the rest of the list."

The head and tail variable names are not keywords here, they could be anything. Try replacing the last case with [foo,] => addUpLength(bar, currentTotal + 1).

This also shows how you can declare a utility function inside a function. Here we're passing the total along in the parameter to the addUpLength function to get Tail Call Optimization, which is basically a fancy way of saying that it uses less memory since it accumulates the result in the currentTotal function parameter.

Return every other element in a list

/* loading */

This example is powered by:

  • the "pipeline operator" |> from the Pervasives standard library Direct from the docs: "Reverse-application operator: x |> f |> g is exactly equivalent to g(f(x))," or, said another way, the output of x is put into f, and the output of f is put into g.
  • List.iter, which takes a function to execute on each item of an iterable, as well as a list to iterate over. In this case, the list to iterate over gets passed in via |>, and the function to execute on each element is Js.log.

Sum over a list of booleans

/* loading */

This example is powered by pattern matching and the ternery sugar. Many times in ReasonML, the "batteries" that are included is just the pattern matching.

List Searching

Here are some examples of searching lists.

List.find - get first item from list that matches

/* loading */

This example is powered by:

List.filter a list of strings by length

/* loading */

This example is powered by:

List.filter a list of strings by a search string

/* loading */

In addition to |>, filter and iter from above, this example is powered by:

  • Js.Re from bucklescript, which is linked to as the appropriate regex solution in the Reason docs.
  • currying, when we called str1InsideStr2(s1, s2) twice, first with the parameter "San" to search for, and second as the callback to filter.

List: Explorations

Write a function that, given a list,

  1. ... and a number n, returns the first n items of the list.
  2. ... and a number n, returns the last n items of the list.
  3. ... returns the list reversed.
  4. ... returns the palindrome of that list.
  5. ... and an item i, returns the first index of i in the list, and -1 otherwise.

Image Credit: List by Nik Stanbridge on Flickr

(Edit this post here!)