Want this course?
We continue the gotchas for lazy sequences. What if you want to print an infinite list? What happens? Does smoke come out of your computer, like in the movies? Get your heat sinks set up because printing an infinite sequence will take infinite memory, energy, and time! Thanks, physics 🙁
Luckily, Clojure has a compromise. You can’t know if a list is infinite until you find the end, but you can tell Clojure when to quit. There’s a global variable called print-length that will save you from locking up your repl when you print long lists.
The idea of this compromise has never sat well with me. What if
*print-length* is set to 100, but there are 101 elements in the list? is there a better solution? Write down in your own words what the algorithm is for enforcing
*print-length*. How could the algorithm be made better (if it can)?