I have this code:
pairs :: [a] -> [(a,a)]
pairs [] = []
pairs xs = zip xs (tail xs)
An example of what this code does:
> pairs "hey"
[('h','e'),('e','y')]
> pairs [1..7]
[(1,2),(2,3),(3,4),(4,5),(5,6),(6,7)]
I was wondering how to do this in recursive ways. I tried this,
pairs (x:xs) = (x,head xs): pairs xs
but it failed with the following exception
> pairs [1..7]
[(1,2),(2,3),(3,4),(4,5),(5,6),(6,7),(7,*** Exception: Prelude.head: empty list