The pairs function needs to do something like this:
pairs [1, 2, 3, 4] -> [(1, 2), (2, 3), (3, 4)]
Use parentheses and commas to create tuples. Use one comma to create a pair. Use more commas to create tuples with more components. Note that it is also possible to declare tuples using in their unsugared form.
The most basic way of defining a function in Haskell is to ``declare'' what it does. For example, we can write: double :: Int -> Int double n = 2*n. Here, the first line specifies the type of the function and the second line tells us how the output of double depends on its input.
The == is an operator for comparing if two things are equal. It is quite normal haskell function with type "Eq a => a -> a -> Bool". The type tells that it works on every type of a value that implements Eq typeclass, so it is kind of overloaded.
Use the fst and snd functions (from Prelude or Data. Tuple ) to extract the first and second component of pairs. Or use pattern matching. Pattern matching also works for tuples with more than two components.
pairs [] = []
pairs xs = zip xs (tail xs)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With