Looking to learn more about functional programming?
Well, in 2024 your go-to option is OCaml.
Today we are going to explore OCaml and see why it's great to write and learn functional programming.
Here is how to get started 👇
Tech stack
- OCaml: OCaml has a great ecosystem of packages and build tools all designed to work well together. Easy to get started!
Setup
Installing OCaml requires running a single command:
bash -c "sh <(curl -fsSL https://raw.githubusercontent.com/ocaml/opam/master/shell/install.sh)"
You then need to install and setup opam
and dune
to have a complete developer environment with formatting, auto complete, and package management all in one.
More details about the installation process in the full article linked below 👇
Get started
OCaml files use the .ml
extension. The "Hello World" of OCaml is the following:
let () = print_endline "Hello, World!"
OCaml embraces functional programming. The key features are:
- All variables are immutable
- "Mostly pure"
- Function composition
- Functional types like Option and Either
- Powerful pattern matching
There is more 🤩
Timeless coding principles, practices, and tools that make a difference, regardless of your language or framework, delivered in your inbox every week.
Implementation
Let's solve a puzzle to learn how to write OCaml code:
Given a string, combine the first and last digit to form a single two-digit number.
For example 1abc2
(12), pqr3stu8vwx
(38), a1b2c3d4e5f
(15), treb7uchet
(77).
The first step in any functional programming program is defining types:
type digits =
| Empty
| Full of int * int
A type
represent a data structure. In this example we have 2 states: Empty
and Full
(which contains a tuple of 2 int
).
Every program is built from smaller functions composed together.
Another powerful feature typical of functional languages is pattern matching:
let digits_sum dig =
match dig with
| Empty -> 0
| Full (firstNum, lastNum) -> (firstNum * 10) + lastNum
We use match ... with
to return some value for each state:
- Empty: No digits available, return 0
- Full: We have both digits so we can combine them
Pattern matching is even more powerful when working with multiple variables:
let collect_digits dig str =
let int_option = int_of_string_opt str in
match dig, int_option with
| Empty, None -> Empty
| Empty, Some n -> Full (n, n)
| Full (f, l), None -> Full (f, l)
| Full (f, _), Some n -> Full (f, n)
Here we pattern match on both dig
and int_option
. OCaml will inform us if we forget to match a possible case.
Finally, another great (and super satisfying) feature of functional programming is piping:
let program source = source |> read_chars |> digits_sum
Look how beautiful this code looks: a clear and readable sequence of steps (do this, then this, then this 🪄)
To recap:
- Define types (immutable data structures)
- Organize program in small functions
- Use features like
option
,either
, and pattern matching to implement the solution - Compose all together using pipes
This is the essence of functional programming. OCaml is built on top of these concepts. This is what makes it great for learning functional programming!
👉 For all the details and code snippets you can read the full article containing all the details of the implementation.
Takeaways
- In 2024 OCaml is your best choice to get started learning and writing functional programming code
- OCaml works great with VSCode: auto formatting, auto completion, error reporting, snippets, documentation (it's just a great experience)
- With OCaml you will experience the satisfaction of "If it compiles it works" 🪄
- You can practice writing OCaml code and then converting it to the equivalent in other languages to test your understanding of functional programming
Functional programming? Go with OCaml. My suggestion is always to start writing real code, you will learn way more than just reading or studying about it.
That's it for this week project. Next week is going to be a special one, stay tuned!
See you next 👋