In chapter 5, you’ll develop the process of reversing the list by implementing fold- Left in terms of foldRight, and foldRight in terms of foldLeft. But this shows that the recursive implementation of foldRight won’t be optimal because reverse is an O(n) operation: the time needed to execute it is proportional to the number of ele- ments in the list, because you must traverse the list. By using reverse, you double this time by traversing the list twice. The conclusion is that when considering using fold- Right, you should do one of the following:
Not care about performance
Change the function (if possible) and use foldLeft
Use foldRight only with small lists
Use an imperative implementation
In chapter 5, you’ll develop the process of reversing the list by implementing fold- Left in terms of foldRight, and foldRight in terms of foldLeft. But this shows that the recursive implementation of foldRight won’t be optimal because reverse is an O(n) operation: the time needed to execute it is proportional to the number of ele- ments in the list, because you must traverse the list. By using reverse, you double this time by traversing the list twice. The conclusion is that when considering using fold- Right, you should do one of the following: Not care about performance Change the function (if possible) and use foldLeft Use foldRight only with small lists Use an imperative implementation