As a beginner exercise, I’ve implemented the following function to find the n-th element in a list:
elem_at (h:_) 0 = h
elem_at (_:t) n = elem_at t (n-1)
elem_at _ _ = error "Index out of bounds"
However, if I call: elem_at [1,2,3,4] 5, is it correct that it will return “Index out of bounds” only after having traversed the whole list, so that the last line matches the pattern _ _ with [] 1? More generally, if the list were big wouldn’t that be a performance issue? Can ghc somehow optimize that situation?
In fact, this is precisely the canonical way to index a list, almost. You need to add in checking for negative numbers
And you could add a specific match for the empty list:
And the base and inductive case:
and you’ve got the Prelude definition of list indexing, the
(!!)operator.A slightly more efficient version can be yielded via the worker wrapper, floating the index test to the top level.