An important point to understand about recursive function calls is that just as they “wind up” as they are called repeatedly, they “unwind” rapidly when the function’s end condition is reached.
In the case of the
sum function, the end condition is reached when the
Nil element in a
List is reached. When
sum gets to the
Nil element, this pattern of the
match expression is matched: