Fast and reliable programs with OCaml
Pattern Matching
OCaml's version of switch
is match
. Here, low numbers print as words, but anything else uses the built-in string_of_int
function to display a decimal:
let to_string x = match x with | 1 -> "one" | 2 -> "two" | n -> string_of_int n
You can also match on strings, variants, records, and other complex values. OCaml will always check that you have covered every case. This is the key to its reliability.
Pattern matching can also be used in anonymous functions, like this:
let to_string = function | 1 -> "one" | 2 -> "two" | n -> string_of_int n
Anonymous functions are like lambda expressions in Python; you'll often see this shorter form.
Data Structures
OCaml allows you to define both variants (unions representing a choice of alternatives) and records (structs or named tuples holding several values at once). For example, the result of a download can be declared as a variant:
type download_result = | Success of filepath | Network_error of string | Aborted_by_user
This ensures that everywhere a URL downloads, it also handles a failed download (typically by displaying the error to the user) or a canceled download (not an error).
It's used as follows:
match download url with | Success file -> process file | Network_error msg -> alert msg | Aborted_by_user -> ()
Notice that each case can only access the appropriate value: You can't accidentally try to use file
if the download failed or display msg
if it succeeded.
A record joins together several pieces of information:
type program = { name : string; homepage : url option; }
Records and variants can be combined. In fact, the type url option
in this record is itself a variant, because the homepage
field can be either None
or Some
URL. By making optional types explicit, OCaml completely avoids Java's dreaded NullPointerException
(or C's segfault
).
OCaml also supports polymorphic (generic) types:
type 'a result = | Success of 'a | Error of exn
These allow you to construct whole families of types (e.g., int
result, string
result, or program
result), while still writing generic code that can work on any of them, such as this function to return a result's value (or throw its exception):
let success = function | Success value -> value | Error ex -> raise ex
You don't have to declare variants before use; you can use the syntax `<tag>
to let OCaml infer variant types automatically (examples below).
Loops
An unusual feature of functional programming is that loops are often written in a tail-recursive style (Listing 1). This prompts the user with a question, returning `yes
if the user enters y or yes and so on.
Listing 1
Tail-Recursive Loops
If the user doesn't provide a valid response, it calls itself recursively to ask again. Because this recursive call is always the last thing the function does, OCaml does not need to store the return address on the stack, and you can loop any number of times without running out of memory. Effectively, the function call turns into a goto.
OCaml does support more traditional for
and while
loops, too, but the recursive form can be more elegant.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.
-
DebConf24 to be Held in South Korea
Busan will be the location of the latest DebConf running July 28 through August 4