Fun and Functions with Gleam

Overview


Gleam is a statically typed, functional programming language that emphasizes type safety and performance. It runs on the Erlang virtual machine BEAM, and uses the same highly scalable concurrency model as Erlang. In this part, we introduce Gleam, covering its syntax, type system, and features, with a strong emphasis on functional programming.

Under the hood Gleam compiles to Erlang (and more recently JavaScript), and therefore, if required, it is suited to interface with Erlang libraries. Gleam does not have its own execution model, so reasoning about how Gleam programs work is really just reasoning about how Erlang works — so, as a bonus, while learning Gleam, you’re actually learning a bit of Erlang as well ❤️.

At the same time, there’s a bonus because Gleam, unlike Erlang or JavaScript, is a typed language with a nice type system1. The code goes through a type-checker and is therefore more constrained, making reasoning about it a bit easier than reasoning about e.g. Erlang or JavaScript.

All things considered, Gleam is chosen as the language to look into as the emerging functional language of choice for this course as it nicely packs together functional programming, algebraic data types and a familiar syntax.

The structure of this part is as follows:

  • Gleam Basics and Tooling outlines the “standard” programming language features in Gleam, including variables and functions, also briefly contrasting the language with Dart. At the end of the chapter, a brief outline on how to use Gleam locally is provided.

  • Fun and Functional Programming covers functional programming concepts with Gleam.

  • Algebraic Data Types and Data Constructors goes over how you can declare new algeraic data types in Gleam and how to deconstruct data into original components. We will also start playing around with lists more.

  • Recursion and Recursive Thinking is the most important chapter in this part as it covers the arguably most important paradigm of (functional) programming — recursion. There, most programmers have to shift their understanding of programming from an imperative/engineering mindset into a more mathematical one.

  • Generics and Type Level Abstractions covers type-level variables and how they are one of the most useful and productive features of (functional) programming.

  • Modules and Dependencies looks at how larger Gleam software projects are built and how to import code from external dependencies.

  • Back to Programming: Matching parentheses covers how to solve an “actual problem” with Gleam.

Finally, at the end, the chapter Recap and Feedback provides a summary of the part and asks for feedback on the materials.

After this part you are ready to learn the **foundations of mathematics** … or, foundations of programming language design with the next part being a hands-on tutorial going over foundational programming language features one by one.

Additional resources

Due to Gleam being a very modern and emerging programming languages, finding documentation and help for it can be difficult. Below is a compiled list of resources that may be useful in addition to internet searches. ChatGPT might also be able to help but its knowledge cutoff may cause problems.

For extra reading, see e.g.

Footnotes

  1. Gleam’s type system is still quite simple and does not have higher-rank polymorphism or type classes unlike in Haskell for instance.