Divide it into two main steps. First, the function replicates tail (n-1) times. So you get something like
elementAt''' xs n = head $ foldr ($) xs [tail, tail, tail, ..., tail]
Now the definition of foldr in the list expands something like this:
foldr fx [y1, y2, y3, ..., yn] = (y1 `f` (y1 `f` (... (yn `f` x))) ...)
So this fold will expand to (replace f with $ and all y with tail )
foldr ($) xs [tail, tail, tail, ..., tail] = (tail $ (tail $ (tail $ ... (tail xs))) ... ) {- Since $ is right associative anyway -} = tail $ tail $ tail $ tail $ ... $ tail xs
where there are (n-1) tail calls made up together. After taking n-1 tails, it simply extracts the first element from the remaining list and gives back.
Another way to write it that will make the composition more explicit (in my opinion) is to be
elementAt n = head . (foldr (.) id $ replicate (n-1) tail)
source share