It not only seems that the lazy programmer gets the job, but also the lazy programs these days. I first encounter Python’s laziness in my discovery of Python’s usages of generators. This is especially true in modern Python, a.k.a. Python 3, where lots of functions return generators in favor of lists. I found them to be extreme useful for those one-off lists to iterate on.

Lately I started teaching myself Clojure, a dialect of the Lisp programming language. Yet again I was astound by the usage of laziness in programming languages. Lazy evaluation is actually quite a normal thing in functional programming. (See wiki.) In Clojure, it’s very easy to write lazy sequences. I’ll show you later in this post. (Maybe I’ll write another post for the lazy-seq macro itself. We’ll see.)

I won’t try to compare the two programming languages. This post merely tries to demonstrate the usage of laziness in these two languages, side-by-side. This isn’t a comparison of performance or pros-and-cons. However, feel free to visually compare the two. Which one is prettier to you? We’ll start from some basics, and then make incremental progress toward writing a triangular number generator, which led to a simple triangular number checker function.

Range

Let’s start with the a sequence of finite numbers. If the length is deterministic, in Python we wrote:

In Clojure:

Easy Cheesy.

Infinite Sequence

How about today we want to generate an infinite sequence of numbers? In Python, you can use the built-in itertools module, which is a great module to do various operations with iterables:

Alternatively, you can write a generator fairly expressively using the yield keyword:

The later solution is better IMO, because the hand-crafted generator is easier to read when complexity starts piling up. We’ll stick with the generator for the rest of the examples.

Let’s take a look at Clojure. When the range function is called as a zero-arity function (without arguments,) it generates infinite sequence of numbers. Unless you want to blow up the Clojure REPL, don’t try to evaluate it in your Clojure REPL:

You definitely don’t need to hand-crafted the range lazy-seq yourself, but here’s how you do it in Clojure:

We define the number-generator function as a multi-arity function so it’s easier to initiate the function from 0. (See line 6 where we don’t have to provide any argument to kick off the function.) Inside the lazy-seq macro, you may recursively call the function itself without running into stack overflow error.

Infinite Triangular Number Sequence

The definition of Triangular Number on wiki:

“The nth triangular number is the number of dots in the triangular arrangement with n dots on a side, and is equal to the sum of the n natural numbers from 1 to n.”

In Python, let’s progress with the generator function by capturing the n in the generator scope and yield the triangular number in every step:

In Clojure, we’ll use lazy-seq to recursively pass in the new n number into the next call:

Triangular Number Checker

Now we have the triangular number generator, why is this useful? Because you could use this to check if a number is a triangular number. Not so appealing, I know. For the sake of demonstration, let’s pretend this is very interesting. Also, let’s pretend we restrict ourselves from checking the triangular number by the formula.

In Python, I will write a function to loop over the triangular numbers:

However, in Clojure, I would favor function composition:

This is how to create and use lazy code in Python and Clojure.