Are you familiar with slice notation? lst[1:]
will create a slice of the list from [1]
to the end, therefore excluding the [0]
element. So basically it will count 1 for the current element, then recursively call with the rest of the list, removing the front each recursive call. When it is finally called with an empty list it just returns 0 which is the base case.
You could imagine this sequence of calls looking something like
length([1,2,3,4])
1 + length([2,3,4])
1 + 1 + length([3,4])
1 + 1 + 1 + length([4])
1 + 1 + 1 + 1 + length([])
1 + 1 + 1 + 1 + 0