A tuple is an immutable grouping of unnamed but ordered values. Tuples can hold any (or multiple) data type(s) -- including other tuples.
Tuples are defined as comma-separated values between (
and )
characters: (<element_1>, ... , <element_n>)
.
("one", 2) // Tuple pair (2 values)
("one", 2, true) // Tuple triplet (3 values)
Only tuples with the same length and the same types (in the same order) can be compared. Tuples have structural equality, which means they are equal only if all their values are equal.
(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
// compiler error
(1, 2) = (1, 2, 3) // Different length
// compiler error
There are three ways in which you can extract values from a tuple:
fst
and snd
functionslet person = ("Jordan", 170)
// Option 1: fst/snd
let name1 = fst person
let length2 = snd person
// Option 2: deconstruction
let (name2, length2) = person
// => name2 = "Jordan"
// => length2 = 170
// Option 3: pattern matching
match person with
| name3, length3 -> printf "%s: %d" name3 length3
Azara and Rui are teammates competing in a pirate-themed treasure hunt. One has a list of treasures with map coordinates, the other a list of location names with map coordinates. They've also been given blank maps with a starting place marked YOU ARE HERE.
Azara's List | Rui's List | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
But things are a bit disorganized: Azara'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 fsharpies, 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 getCooordinate()
function that takes a (treasure, coordinate)
pair from Azara's list and returns only the extracted map coordinate.
getCoordinate ("Scrimshaw Whale's Tooth", "2A")
// "2A"
Implement the convertCoordinate()
function that takes a coordinate in the format "2A" and returns a tuple in the format (2, 'A')
.
convertCoordinate "2A"
// (2, 'A')
Implement the compareRecords()
function that takes a (treasure, coordinate)
pair and a (location, coordinate, quadrant)
record and compares coordinates from each.
Return true
if the coordinates "match", and return false
if they do not.
Re-format coordinates as needed for accurate comparison.
compareRecords ("Brass Spyglass", "4B") ("Seaside Cottages", (1, 'C'), "blue")
// false
compareRecords ("Model Ship in Large Bottle", "8A") ("Harbor Managers Office", (8, 'A'), "purple")
// true
Implement the createrecord()
function that takes a (treasure, coordinate)
pair from Azara's list and a (location, coordinate, quadrant)
record from Rui's list and returns (coordinate, location, quadrant, treasure)
if the coordinates match.
If the coordinates do not match, return a tuple of the same shape but filled with ""
.
Re-format coordinates as needed for accurate comparison.
createRecord ("Brass Spyglass", "4B") ("Abandoned Lighthouse", (4, 'B'), "Blue")
("4B", "Abandoned Lighthouse", "Blue", "Brass Spyglass")
createRecord ("Brass Spyglass", "4B") ("Seaside Cottages", (1, 'C'), "Blue")
("", "", "", "")
Sign up to Exercism to learn and master F# with 15 concepts, 134 exercises, and real human mentoring, all for free.