So far, we have implemented a linked list and a stack as a list. One of the key concepts in FP is the concept of lazy evaluation. We can make our list lazy so that the elements will be evaluated once we access them. We need to change node in such a way that it will return a function containing list as next, instead of the list itself. The function will be evaluated when it is called; therefore, our list will be lazy.
We start with modifying our node case. In our LinkedList example, next was of the LinkedList<Element> type. To make our list lazy, we will modify next to be a function that returns our list:
enum LazyList<Element: Equatable> {
case end
case node(data: Element, next: () -> LazyList<Element>)
}
As we can see in the preceding code, our node case is not defined as indirect because next is not of the LazyList type and is a reference to a function that returns LazyList...