The ability for something to be defined in terms of itself is called recursion. In Gleam, recursion is most commonly found in recursive functions, which are functions that call themselves.
A recursive function needs to have at least one base case and at least one recursive case. A base case returns a value without calling the function again. A recursive case calls the function again, modifying the input so that it will at some point match the base case.
pub fn factorial(x: Int) -> Int {
case x {
// Base case
1 -> 1
// Recursive case
_ -> x * factorial(x - 1)
}
}
Gleam has no special syntax for looping, so all looping is done with recursion.
pub fn list_length(list: List(String)) -> Int {
case list {
[] -> 0
[_, ..rest] -> 1 + list_length(rest)
}
}
Gleam also supports recursive custom types. A recursive custom type has one or more of its variants refer to itself in their contained data.
pub type RussianDoll {
Child // Base case
Mother(RussianDoll) // Recursive case
}
let very_big_doll = Mother(Mother(Mother(Child)))
let small_doll = Mother(Child)
Izzy is an avid bird watcher that keeps track of how many birds have visited her garden on any given day.
She's asked you to help bring her bird watching to a new level and implement a few tools that will help her track and process the data.
You have chosen to store the data as a list of integers. The first number in the list is the number of birds that visited your garden today, the second yesterday, and so on.
Implement the bird_count.today
function. It should take a list of daily bird counts and return today's count. If the list is empty, it should return 0
.
bird_count.today([2, 5, 1])
// -> 2
Implement the bird_count.increment_day_count
function. It should take a list of daily bird counts and increment the today's count by 1. If the list is empty, return [1]
.
bird_count.increment_day_count([4, 0, 2])
// -> [5, 0, 2]
Implement the bird_count.has_day_without_birds
function. It should take a list of daily bird counts. It should return True
if there was at least one day when no birds visited the garden, and False
otherwise.
bird_count.has_day_without_birds([2, 0, 4])
// -> True
bird_count.has_day_without_birds([3, 8, 1, 5])
// -> False
Implement the bird_count.total
function. It should take a list of daily bird counts and return the total number that visited your garden since you started collecting the data.
bird_count.total([4, 0, 9, 0, 5])
// -> 18
Some days are busier than others. A busy day is one where five or more birds have visited your garden.
Implement the bird_count.busy_days
function. It should take a list of daily bird counts and return the number of busy days.
bird_count.busy_days([4, 5, 0, 0, 6])
// -> 2
Sign up to Exercism to learn and master Gleam with 36 concepts, 125 exercises, and real human mentoring, all for free.