How to Be Lazy
Functional programs make great use of recursive definitions. A recursive definition consists of two parts:
-
A basis, which explicitly enumerates some members of the sequence
-
An induction, which provides rules for combining members of the sequence to produce additional members
Our challenge in this section is converting a recursive definition into working code. You might do this in several ways:
-
A simple recursion, using a function that calls itself in some way to implement the induction step.
-
A tail recursion, using a function only calling itself at the tail end of its execution. Tail recursion enables an important optimization.
-
A lazy sequence that eliminates actual recursion and calculates a value later, when it is ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access