A list in Gleam is an immutable collection of zero or more values. The values in a list must all have the same type. As lists are immutable, once a list has been constructed, its value can never change. Any functions/operators that appear to modify a list (such as adding an element), will actually return a new list.
Lists can be defined as follows:
let empty = []
let singleValue = [5]
let threeValues = ["a", "b", "c"]
The most common way to add an element to a list is through the spread syntax:
let two_to_four = [2, 3, 4]
let one_to_four = [1, ..two_to_four]
// -> [1, 2, 3, 4]
The gleam/list
module in the Gleam standard library contains many useful functions for working with lists. This module is very commonly used in Gleam code so it is good to be familiar with it.
Lists patterns can be used in case expressions to match on lists and assign contained values to variables:
pub fn describe(list: List(String)) -> String {
case list {
[] -> "Empty list"
[x] -> "List with one item: " <> x
[x, y] -> "List with two items: " <> x <> " and " <> y
_ -> "List with three or more items"
}
}
As well as matching on exact length lists, the spread syntax can be used to match on lists of at-least a certain length:
pub fn describe(list: List(String)) -> String {
case list {
[_, _, ..] -> "List with at least two items"
[_] -> "List with one item"
[] -> "Empty list"
}
}
The spread syntax can also be used to assign the rest of the list to a variable:
pub fn remove_first_item(list: List(String)) -> List(String) {
case list {
// Return the list without the first item
[_, ..rest] -> rest
// There's no first item to remove, return an empty list
_ -> []
}
}
Case expressions should have a pattern for every possible value of the type being matched on, so a final discard pattern (_
) is often used to handle any remaining values.
In this exercise you'll be writing code to keep track of a list of programming languages you want to learn on Exercism.
You have six tasks, which will all involve dealing with lists.
To keep track of the languages you want to learn, you'll need to create a new list. Define the new_list
function that returns a new, empty list.
new_list()
// -> []
Currently, you have a piece of paper listing the languages you want to learn: Gleam, Go, and TypeScript. Define the existing_list
function to return this list.
existing_list()
// -> ["Gleam", "Go", "TypeScript"]
As you explore Exercism and find more interesting languages, you want to add them to your list. Implement the add_language
function to add a new language to the beginning of your list.
add_language(["OCaml", "Elixir"], "Scheme")
// -> ["Scheme", "OCaml", "Elixir"]
Counting the languages one-by-one is inconvenient. Implement the count_languages
function to count the number of languages on your list.
count_languages(["jq", "Elm", "Rust", "Kotlin"])
// -> 4
At some point, you realize that your list is actually ordered backwards! Implement the reverse_list
function to reverse your list.
reverse_list(["Python", "Julia", "Idris", "COBOL"])
// -> ["COBOL", "Idris", "Julia", "Python"]
While you love all languages, Gleam has a special place in your heart. As such, you're really excited about a list of languages if:
Implement the exciting_list
function to check if a list of languages is exciting:
exciting_list(["Lua", "Gleam"])
// -> True
Sign up to Exercism to learn and master Gleam with 36 concepts, 125 exercises, and real human mentoring, all for free.