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 ...

Get Programming Clojure, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.