// I need to

ModernizeUpgrade without breaking it RescueFix what someone else started Build NewTurn an idea into a product Reach More PeopleGet found, get used, include everyone

// Company

Case StudiesExpertiseAbout + CareersResourcesContact Book a call →
≡articleBuild New

You Got LISP in my Python!

Hy is a dialect of Lisp embedded in Python that compiles to the Python AST, so you get Lisp syntax with all of Python's libraries. A talk and slides to start.

Art+Logic Jul 8, 20132 min read

From the Art+Logic archive · written in 2013. The tools may have moved on; the thinking still holds.

Cuddles the LISP mascot typing on the keyboard of a laptop

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)!

keep going

More to explore in Build New.

All Build New resources →
let's talk

Got a problem like this one?

Tell us what's stuck. A real person replies within one business day.

Talk to our team →

Talk to people who know software, not a sales script.

Your first call is with Aja, our VP of Sales. He'll help make sure you're solving the right problem. Here's what you'll cover together:

  1. What you're trying to accomplish

    The business goal first, then the technology.

  2. Possible approaches

    And the tradeoffs of each, in plain language.

  3. Risks you may not have considered

    Technical and business, before they cost you.

  4. Whether AI changes the math

    What's practical, and what's just hype for your project.

  5. Sensible next steps

    Even if the right next step is not us.

book-a-call.ics

Loading the scheduler…

Open the booking page →

Some problems are easier to explain in writing. Send us a few lines about what you're working on, and we'll reply within one business day. Helpful to include:

  1. What the software does, or should do
  2. What's stuck, or what's worrying you
  3. Any deadline or budget you have in mind
note.md

Loading the form…