<p>See <a href="https://arxiv.org/abs/1210.2610" rel="nofollow noreferrer">https://arxiv.org/abs/1210.2610</a>, page 5. Here is some example code:</p>

<pre><code>from itertools import chain, count
from functools import lru_cache

@lru_cache(maxsize=None)
def terms(size, level=0):
    if size == 0:
        return tuple(range(level))
    else:
        abstractions = (
            ('abs', term)
            for term in terms(size - 1, level + 1)
        )
        applications = (
            ('app', term1, term2)
            for i in range(size)
            for term1 in terms(i, level)
            for term2 in terms(size - 1 - i, level)
        )
        return tuple(chain(abstractions, applications))

def string(term):
    if isinstance(term, tuple):
        if term[0] == 'abs':
            return '(λ {})'.format(string(term[1]))
        elif term[0] == 'app':
            return '({} {})'.format(string(term[1]), string(term[2]))
    else:
        return term

for size in count():
    print('{} terms of size {}'.format(len(terms(size)), size))
    for term in terms(size):
        pass # input(string(term))
</code></pre>

<p>This outputs</p>

<pre><code>0 terms of size 0
1 terms of size 1
3 terms of size 2
14 terms of size 3
82 terms of size 4
579 terms of size 5
4741 terms of size 6
43977 terms of size 7
454283 terms of size 8
</code></pre>

<p>and so on (i.e. <a href="http://oeis.org/A220894" rel="nofollow noreferrer">this sequence</a>).</p>
What is an algorithm to enumerate lambda terms?
<p>What is an algorithm that will enumerate expressions for the lambda calculus by order of length? For example, <code>(λx.x), (λx.(x x)), (λx.(λy.x))</code> and so on?</p>