I tried looking up Int -> [a] -> Maybe a
on hoogle, but no luck.
I feel like this should be in a standard library somewhere, but I don't know where.
There is a library called Safe on Hackage which holds a function:
atMay :: [a] -> Int -> Maybe a
There isn't one in the standard library (would be nice, though), but here's one way to implement it (also works for infinite lists):
(!!!) :: [a] -> Int -> Maybe a
[] !!! _ = Nothing
(x:xs) !!! n
| n < 0 = Nothing
| n == 0 = Just x
| otherwise = xs !!! (n-1)
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