blog

Cuddles the LISP mascot typing on the keyboard of a laptop

You Got LISP in my Python!

by

I’ve always been intrigued by the idea that Peter Norvig put forward, that "Basically, Python can be seen as a dialect of Lisp with "traditional" syntax."

Apparently, there are folks who don’t find this sufficient. Enter "Hy". As its creator says:

Hy is a wonderful dialect of Lisp that’s embedded in Python. Since Hy transforms its lisp code into the python Abstract Syntax Tree, you have the whole beautiful world of python at your fingertips, in lisp form!

If this is something that appeals to you (speaking for myself, having a LISP that’s got the full batteries-included libraries that are available for Python is pretty cool and could get me to take another stab at Structure and Interpretation of Computer Programs), you should check out this 47-minute talk by Paul Tagliamonte from a February meeting of the Boston Python group.

http://youtu.be/ulekCWvDFVI

(or just scan through the slides from that talk).

Looking at some samples from the Hy documentation lets us compare the syntactical similarities and differences — this bit of Python code:

def simple_conversation():
    print "Hello!  I'd like to get to know you.  Tell me about yourself!"
    name = raw_input("What is your name? ")
    age = raw_input("What is your age? ")
    print "Hello " + name + "!  I see you are " + age + " years old."
simple_conversation()

…would be written in Hy as:

(defn simple-conversation []
   (print "Hello!  I'd like to get to know you.  Tell me about yourself!")
   (setv name (raw_input "What is your name? "))
   (setv age (raw_input "What is your age? "))
   (print (+ "Hello " name "!  I see you are "
              age " years old.")))
(simple-conversation)

Why bother?

Again, from the Hy docs:

There are some advantages to having a code structure that’s actually a very simple datastructure as the core of lisp is based on. For one thing, it means that your programs are easy to parse and that the entire actual structure of the program is very clearly exposed to you. (There’s an extra step in hy where the structure you see is converted to python’s own representations… in more “pure” lisps such as common lisp or emacs lisp, the data structure you see for the code and the data structure that is executed is much more literally close.)

Another implication of this is macros: if a program’s structure is a simple data structure, that means you can write code that can write code very easily, meaning that implementing entirely new language features can be very fast. Previous to hy, this wasn’t very possible for python programmers… now you too can make use of macros’ incredible power (just be careful to not aim them footward)!

+ more

Accurate Timing

Accurate Timing

In many tasks we need to do something at given intervals of time. The most obvious ways may not give you the best results. Time? Meh. The most basic tasks that don't have what you might call CPU-scale time requirements can be handled with the usual language and...

read more