The Hy Programming Language

Written by on January 28, 2024 in Hy Language, LISP, Python with 0 Comments

In an earlier article I had explained how to execute Python code from within Common Lisp using “CLPython” package.

In contrast to that approach, “Hy” is a Lisp-style language (not compatible with Common Lisp) that is embedded in Python and hence provides seamless interoperability with Python code.

Installation is straightforward (it is usually a good idea to create a new Conda environment prior to installation). 

pip install —user hy

After installation, make sure to set the PATH environment variable correctly.

The easiest way to get started is through the REPL. Just type “hy” on the command line. We can then start entering Hy expressions, which will be evaluated immediately. See the following image:

Hy REPL

Hy REPL

Let us try something more interesting. How about using the “spaCy” NLP library? Here is the code (VSCode):

spaCy Example

spaCy Example

In Line 10, I define a simple macro that converts “setv” to “setf” (as in Common Lisp). In Line 15, I define a “Class TextProcessor” to make use of “spaCy”. The two methods in it allow us to print Part-of-Speech info for the words in a given piece of text, and to print noun phrases in the text.

The “main” function creates a TextProcessor object, passing the language model to use, and then invokes the two methods with some sample text.

When we run the code in the Terminal from within the VSCode IDE, here is the output:

Program Output

Program Output

As this example shows, using Python libraries in Hy is quite simple and works as expected. The current version of Hy already supports the necessary constructs and functionality to write non-trivial code. We have the familiar “if”, “cond” conditionals, “while”, “for”, “lfor” loop constructs, and “do” as the equivalent of “progn”. At a higher level, we have “class”, “function” and “macro”. There is also the “Hyrule” utility library (this should be installed separately), which brings in a lot of extra functionality (Collections, Sequences, special Macros, etc.) to the Hy environment.

If you have a Lisp (any dialect) background and are familiar with Python, Hy will definitely appeal to you. The language is still evolving, so we can expect many more interesting features in the upcoming releases. 

One major advantage that I see is that since Hy converts the Lisp code directly into Python AST instead of “interpreting” at runtime, there is no loss of efficiency. Because of this we get the thrill of Lisp programming within the amazing ecosystem of Python!

If you would like to learn more about the Hy language, you can go through the official documentation . There is also a nice introductory book written by Mark Watson.

This article is based on Hy 0.28.0 version. 

I definitely hope to spend more time with Hy and write more about it in future articles. Have a nice Weekend!

Tags: , ,

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *

Top