A tuple is an ordered container of values. Like all Gleam data types tuples are immutable. Each element of a tuple can be of a different type -- they can even be other tuples.
Tuples are defined as comma-separated values between #( and ): #(1, 2.0, "Three").
#("one", 2) // Tuple pair (2 values)
#("one", 2, 3.0) // Tuple triplet (3 values)
Tuples with the same length and the same types (in the same order) can be compared for equality.
#(1, 2) == #(1, 2) // Same length, same types, same values, same order
// -> True
#(1, 2) == #(2, 1) // Same length, same types, same values, different order
// -> False
#(1, 2) == #(1, "2") // Same length, different types
// Compile error!
#(1, 2) == #(1, 2, 3) // Different length
// Compile error!
There are three ways in which you can get the contained values out of a tuple:
let.case.let person = #("Jordan", 170)
// Option 1: Indexing
person.0 // -> "Jordan"
person.1 // -> 170
// Option 2: let
let #(name2, length2) = person
// -> name2 = "Jordan"
// -> length2 = 170
// Option 3: case
case person {
#(name3, length3) -> {
name3 // -> "Jordan"
length3 // -> 170
}
}
Aazra and Rui are designing a pirate-themed treasure hunt. There is a list of treasures with map locations, the other a list of place names with map locations.
| Azara's List | Rui's List | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
But things are a bit disorganized: Aazra's coordinates appear to be formatted and sorted differently from Rui's, and they have to keep looking from one list to the other to figure out which treasures go with which locations. Being budding Gleamlins, they have come to you for help in writing a small program (a set of functions, really) to better organize their hunt information.
Implement the place_location_to_treasure_location function that takes a place Location (such as #("C", 1)) and converts it to a treasure location (such as #(1, "C")).
place_location_to_treasure_location(#("C", 1))
// -> #(1, "C")
Implement the treasure_location_matches_place_location function that takes a place location (such as #("C", 1)) and returns True if it matches a treasure location (such as #(1, "C")).
treasure_location_matches_place_location(#("C", 1), #(1, "C"))
// -> True
treasure_location_matches_place_location(#("C", 1), #(2, "C"))
// -> False
Implement the count_place_treasures function, that takes a place (such as #("Aqua Lagoon (Island of Mystery)", #("F", 1))), and the list of treasures, and returns the number of treasures that can be found there.
let place = #("Aqua Lagoon (Island of Mystery)", #("F", 1))
count_place_treasures(place, treasures)
// -> 2
Implement the special_case_swap_possible function, which takes a treasure (such as #("Amethyst Octopus", #(1, "F"))), a Place (such as #("Seaside Cottages", #("C", 1))) and a desired treasure (such as #("Crystal Crab", #(6, "A"))), and returns True for the following combinations:
Sign up to Exercism to learn and master Gleam with 36 concepts, 125 exercises, and real human mentoring, all for free.