Lecture 5-1
Tuples
A tuples allows us to bind two or more values together:
(1,2)
("A", "few", "words")
(6, "six")
- Tuples can have any size but must be at least 2.
- The size should be thought of as being fixed as it is not easy to change the length of a tuple.
- Tuples can mix types.
Example Functions
Taking Tuples as Input
f (x, y) = x
This will take a tuple with the elements x
and y
and will return the first element of the tuple.
Returning Tuples as Output
f x y = (max x y, min x y)
Syntax
f x y = x + y
g (x, y) = x + y
Both will give the same output but the recommendation is to use the Haskell method instead of always passing tuples.
Exercises
-
exercise1 x = (x / 10, 100 * x)
-
exercise2 (x, y, z) = x + y + z
-
exercise3 (x, y, z) = (z, y, x)