Hy is a Lisp on Python

Posted on April 19, 2013

Like any sane programmer, when I first saw paultag’s demonstration of a Lisp he wrote in Python at Pycon 2013, my initial reaction was to chuckle. His lightening talk was light-hearted, technically clever, and his inability to suppress his mirth was infectious. Almost everyone in the audience had a good time.

The name for this programming language is now known officially as, Hy. You can find it on github and on its website where you can try it in your browser. I know what you’re thinking and, yes, you would be correct in thinking that this is insane.

But it is fun!

Somehow on an early April afternoon I found myself on the official IRC channel #hy on irc.freenode.org chatting with Paul and others. A few hours later I submitted my first commit. Today I just submitted another patch to add varargs, keywords, and kwargs function argument definitions. I also wrote my first Hy program.

Why Hy?

This is the very first question I asked myself. And everyone in the channel. I thought about it for quite a while before I submitted a patch. I suspect the answer is going to be different for everyone.

I’ve been programming in Python professionally for over seven years. It’s a great language and for working with teams of programmers it’s my preferred language. As far as dynamic languages go it is pleasantly minimalist and it is bundled with a practical set of libraries. You can get a lot done with just the batteries.

Around 2008 I was introduced to the world of Common Lisp. I had ended up arguing with someone (who is now a good friend) that Lisp was an old, outdated language and that Python was clearly the superior language. And after learning Common Lisp I’ve come to the conclusion that I was wrong. Dead wrong.

As many programmers before me have come to realize, Lisp spoils you. You start to see the holes in other languages that would be neatly filled if only they had x Lisp feature: Homoiconicity, code as data, macros, meta-object protocol, optional dynamic scoping, and so on. It becomes a drag when blub languages pull out yet another “feature,” that was discovered by Lispers decades ago as if it were brand-new. You start to wonder how people get any real work done with these stone axes and bone picks.

Hy seems to be filling a nice space for me. It’s some kind of Lisp but it fully inter-operates with the existing Python ecosystem. There’s the potential there for me to write my Python modules in Hy and still work with other Python programmers without anyone being the wiser. There’s also the potential to finally bring some really neat features from the Common Lisp world to Python: macros, dynamically scoped variables, and who knows — maybe even pattern matching.

And I don’t have to reinvent the wheel either. The entire ecosystem of Python libraries are available to Hy. The scripts and libraries I’ve written are available to Hy. I have no plans to write a brand new, half-baked implementation of Common Lisp and I don’t think any of the Hy developers do either. We have a healthy mix of programmers with backgrounds in Scheme, Clojure, and Common Lisp. The prevailing attitude seems to be pilfering the best ideas wherever we can find them. And bolting them on top of this monstrosity we call Hy.

And it works!

So here’s a little Hy code:

(import-from sh cat grep)
(print "Words that end with `tag`:")
(print (-> (cat "/usr/share/dict/words") (grep "-E" "tag$")))

Pretty cool, no? Well as with any new language there are plenty of little trivial examples. The next steps will be to write more programs in Hy. I added to that library today and I hope more people will too. Hy could be a very beautiful thing and it only gets better with more users.

Besides it’s fun to make things.