Tutorial by Examples

Use parentheses and commas to create tuples. Use one comma to create a pair. (1, 2) Use more commas to create tuples with more components. (1, 2, 3) (1, 2, 3, 4) Note that it is also possible to declare tuples using in their unsugared form. (,) 1 2 -- equivalent to (1,2) (,,) 1 2 3 ...
Use parentheses and commas to write tuple types. Use one comma to write a pair type. (Int, Int) Use more commas to write tuple types with more components. (Int, Int, Int) (Int, Int, Int, Int) Tuples can contain values of different types. (String, Int, Char) Tuples can contain complex ...
Pattern matching on tuples uses the tuple constructors. To match a pair for example, we'd use the (,) constructor: myFunction1 (a, b) = ... We use more commas to match tuples with more components: myFunction2 (a, b, c) = ... myFunction3 (a, b, c, d) = ... Tuple patterns can contain comple...
Use the fst and snd functions (from Prelude or Data.Tuple) to extract the first and second component of pairs. fst (1, 2) -- evaluates to 1 snd (1, 2) -- evaluates to 2 Or use pattern matching. case (1, 2) of (result, _) => result -- evaluates to 1 case (1, 2) of (_, result) => resu...
Use the uncurry function (from Prelude or Data.Tuple) to convert a binary function to a function on tuples. uncurry (+) (1, 2) -- computes 3 uncurry map (negate, [1, 2, 3]) -- computes [-1, -2, -3] uncurry uncurry ((+), (1, 2)) -- computes 3 map (uncurry (+)) [(1, 2), (3, 4), (5, 6)] -- co...
Use the curry function (from Prelude or Data.Tuple) to convert a function that takes tuples to a function that takes two arguments. curry fst 1 2 -- computes 1 curry snd 1 2 -- computes 2 curry (uncurry f) -- computes the same as f import Data.Tuple (swap) curry swap 1 2 -- computes (2, 1...
Use swap (from Data.Tuple) to swap the components of a pair. import Data.Tuple (swap) swap (1, 2) -- evaluates to (2, 1) Or use pattern matching. case (1, 2) of (x, y) => (y, x) -- evaluates to (2, 1)
The pattern (p1, p2) is strict in the outermost tuple constructor, which can lead to unexpected strictness behaviour. For example, the following expression diverges (using Data.Function.fix): fix $ \(x, y) -> (1, 2) since the match on (x, y) is strict in the tuple constructor. However, the fo...

Page 1 of 1