Python (Lambda Only)

Long ago, when Python 3 was being designed, there was heated debate[1] about whether Python 2's lambda keyword should be kept or retired. The following email caused quite a stir in my university's computer science lab due to a claim that Python would not be more expressive if lambda could contain statements:

[python-dev] Let's just *keep* lambda
Guido van Rossum
Thu Feb 9 19:33:10 CET 2006
To those people still complaining that lambda is crippled because it
doesn't do statements: First, remember that adding statement
capability wouldn't really add any power to the language;

Was this true? Could a Python program be written with lambda replacing all uses of def? What would it look like?

It looks like the world's worst Lisp:

import hashlib
import os
import sys

check_path = (lambda filepath, hashes, print_ = sys.stdout.write:
  (lambda hash = hashlib.sha1(file(filepath).read()).hexdigest():
    ((hash in hashes) and (print_("DUPLICATE FILE\n"
                                  "   %s\n"
                                  "of %s\n" % (filepath, hashes[hash])))
     or hashes.setdefault(hash, filepath)))())

scan = (lambda dirpath, hashes = {}:
  map (lambda (root, dirs, files):
    map (lambda filename: check_path(os.path.join(root, filename), hashes), files), os.walk(dirpath)))

((len(sys.argv) > 1) and scan(sys.argv[1]))

I think this could be taken even further by using __import__ and nesting the whole thing inside a top-level lambda, to see what Python would look like without any statements at all. Handling of the variable scoping is a bit tricky though, because parameter defaults can't reference other parameters – you'd have to move a lot more into nested expressions.

In 2009 I confused the denizens of StackOverflow by posting this as a solution. Good thing too, I later lost the original source and had to recover it from my post there.

  1. While looking up old links for this post, I discovered that Patrick Collison (my current CEO) was posting in the discussion thread! Software development is truly a small world.