Maybe list subscripting

Haskell

Public Domain

Like (!!), but wrapped in a Maybe in case the index is beyond the bounds of the list

Download (right click, save as, rename as appropriate)

Embed

1
2
3
infix 9 !?
(!?) :: [a] -> Int -> Maybe a
list !? n = case drop n list of x:_ -> Just x; [] -> Nothing