At it’s core Hy is just a Python with LISP syntax (with some major additions like macroses). It compiles to Python AST, so it is fully compatible with Python ecosystem.
I extensively use it in my hobby projects since 2023.
Code Example
Python:
|
|
Equivalent Hy-code:
|
|
🔗
More examples at: hylang.org
Why using Hy
My personal story in short:
- My first serious programming language was Wolfram Mathematica (WM), which is functional language built on top of term-rewriting core. WM clearly had LISP roots with it’s syntax and metaprogramming capabilities. I moved from WM, since it was unable to ship stanalone apps.
- I then tried vanilla Python, but imperative syntax and paradigm felt somewhat cumbersome to me.
- I then dived into top-of-the-food-chain functional language Haskell, but being unable to adequately setup GUI libs on Windows was a big turnover.
- Then Hy came to the rescue with its benefits described below...
Benefits of Hy:
➕
Hy has full access to Python ecosystem:
- Almost any existing Python library can be used in Hy
- Arbitrary Python code can be called from Hy like so:
(py "a = 3 + 4")
- Hy code can be called from Python
- Any Python code (from tutorials for example) can be easily converted into Hy
- Hy code can be REPLed in IPython session and Jupiter notebooks
- Hy is implemented just as Python library; not as standalone language
Perhaps, some esoteric libs (that direcly manipulate AST) might not work as intended in Hy. However even still, those problematic parts of projects can be written in Python, and the rest be kept in Hy. But realistically I never had such a problem (I mostly use NN, functional, scientific and GUI libraries).
➕
Hy is a good base for extending Python into functional paradigm:
- Hy has LISP syntax (S-expressions), which is considered convenient for functional paradigm. It is also convenient for lexing and parsing Hy code itself, if you are into those kinds of things.
- Hy has macros system — it enables famous LISP metaprogramming (code as data). It can extend the language, automate repetitive boilerplates, etc.
➕
Compared to other LISPs, being able to call Python code is a nice way of writing math expressions.
Like (py "math.sqrt(x**2 + y**2)/2")
is much clearer than (/ (math.sqrt (+ (** x 2) (** y 2)) 2)
, as you would write in other LISPs ouf of the box.
➕
Since Hy is just a Python in a diguise, skills you aquire in Hy directly translate to Python skills (aside from syntax and macroses obviously). So, by learning Hy you do invest into smth with actual market value (I mean Python).
Due to extensibility of Python itself, I use functional libs, that make Python (and thus Hy) much more functional:
- funcy (or toolz) — libs that provide list manipulation features
- returns — enables Monads (Maybe, IO, Either, ...)
- lenses library — implements Haskell-originated approach for digging into deeply nested data-structures
- some other libs that provide immutable collections
Still, there also some drawbacks to Hy:
➖
Hy supports type hinting syntax, but type checkers (like mypy) will not work with Hy (out of the box at least).
I believe some workaround must be possible, like using typechecker that works on AST (and not on Python code), or maybe building runtime type checks macroses... I didn’t dig deeply in this direction yet, just shared some ideas.
➖
Hy has no proper IDE support: syntax files are available, but no LSP currently.
It is a common theme for niche languages. Not that bad once you get used to it.
➖
Hy has relatively small (niche) community.
To be honest, once you learn the syntax, most problems will be Python-problems (or lib-problems), not Hy-problems. And even if you can’t solve some Hy problem, you still can simply use Python code.
Summary of my personal reasons to use Hy
- Direct access to Python ecosystem (use your dear PyTorch and PyQt with Hy)
- Knowledge transfer from Hy to Python (you keep your market value)
- Python extensibility combined with Hy LISP-like syntax and macros is a good base for tuning Hy/Python into functional paradigm (by leveraging functional Python libraries)
Hy drawbacks that I can live with:
- No proper IDE/LSP support but, Vim plugins can compensate for it
- Small/niche community, but access to Python ecosystem compensates it
- Mypy doesn’t work with Hy out of the box, but I believe it can be done