List recursion
While it is more common to use functions in the
gleam/list
module to iterate across a list, at times you may prefer to work
with the list directly.
The [first, ..rest]
pattern matches on a list with at least one
element, assigning the first element to the variable first
and
the rest of the list to the variable rest
. By using this pattern
and a pattern for the empty list []
a function can run code on
each element of a list until the end is reached.
This code sums a list by recursing over the list and adding each int to a
total
argument, returning it when the end is reached.