I am learning some Haskell, and I am trying to get my head around how pattern matching works.
In doing so, I have written a simple nth function.
nth' :: Integer -> [a] -> a
nth' n [] = error "Index out of bound"
nth' n (x:xs) = if n == 0 then x else nth' (n - 1) xs
This first implementation seem to work as expected.
-- nth' 25 ['a'..'z']
-- 'z'
-- nth' 26 ['a'..'z']
-- *** Exception: Index out of bound
However, when I refactor it replacing the if statement with pattern matching, I end up getting the "Index out of bound" exception where I clearly should not.
nth' :: Integer -> [a] -> a
nth' _ [] = error "Index out of bound"
nth' 0 (x:[]) = x
nth' n (_:xs) = nth' (n - 1) xs
-- nth' 2 ['a'..'z']
-- *** Exception: Index out of bound
What am I doing wrong?