Do you need help with developing a great blog, website, or an app? Findout more and get a free consulstation →


Introduction to Hy - Lisp in Python.


Many people consider lisp to be the most powerful language - which to me, with my obsession with the perfect tools, sounds very appealing. Lisp is also considered the "language of AI" - and that makes it even more interesting.

Hy lisp is a lisp written on top of python, and compatible with it in both directions - you can seamlessly use hy from python or python from hy. That lets you to use all the power of python and its libraries with the elegance and features of lisp - including macros(more on that later).

In this tutorial I want to tell you about the very basics of this language(so that I could dive deeper into more advanced topics in my future tutorials).


Installation is very simple:

pip install hy
If you're an emacs user - also install hy-mode

To run Hy commands you simply type:


And that will open REPL - read, eval, print loop. The interactive lisp environment, similar to the python shell.

Or you can type:

hy /path/to/file.hy

to execute existing file that you've created.

Hello world

Lets start as usual, with writing a simple "hello world" program. To print some phrase on the screen you simply type:

( print "Hy is awesome!" )
"Hy is awesome!"

Simple expression

All lisp programs consist of lists - which are expresions enclosed in parentheses.Here's a simple expression:

( + 2 3 )

Here the '+' sign is an operator, and 2 and 3 are the arguments we're passing to it.

As you can guess this expression simply sums 2 numbers. It may seem a bit unusual to write '+' sign before the arguments, instead of "2 + 3", but this has several benefits, for example you can pass more than one argument, like so:

( + 2 3 4 )

This is called "prefix notation", you'll get used to it very quickly and realize that it is very convenient and cool =)

Lists and basic operations on them.

Lists are the fundamental data type in lisp(lisp stands for list processing).

Lists can contain other lists(this is called 'a nested list'):

( + 2 ( / 6 2 ) ( - 16 8 ) )

When a list is evaluated, it's elements are evaluated from left to right, and their values are passed to the first argument(usually a function).

You can turn off the evaluation of a list by putting a quote(') sign before it. When you quote a list, you get back the list itself:

' ( + 2 3 )
(+ 2 3)

You can access the first element of a list using the function 'car', and you can access the rest of the list using 'cdr':

( car ' ( a b c ) )

( cdr ' ( a b c ) )
('b' 'c')

You can use cons to build lists. It will return a list with a new element on the front:

( cons ' ( a ) ' ( b c ) )
('a' 'b' 'c')

The empty list is represented by the symbol nil, which is also the value that represents 'false' in lisp.

If you want to create a list with many elements you can use 'list*':

( list* 'a 'b 'c 'nil )
('a' 'b' 'c')

To assign a value to the variable use:

( setv a 13 )

Defining functions

This is how you define a function in hy:

;;; Function that returns the average of 2 numbers
( defn average [a b]
( / ( + a b ) 2 ) )

( average 2 4 )

To defn you first supply the name of the function, than its arguments, and than the body of the function. Function will return the result of evaluating it's last expression.

By the way, as you can see, the comments in hy start with semicolon. The convention is to use 4 semicolons at the beginning of the file, 3 to describe the following big block of code, 2 to describe a smaller block of code (something inside of the function for example, and you use one semicolon to create a comment at the end of the line.

Which is very clever and convenient, and a good text editor, such as emacs, will automatically intend everything properly.


The standard conditional operator is 'if':

( if ( > 2 1 ) ; Is this statement true?
( print "True" )
( print "False" ) )

It evaluates the first expression that you pass to it, if it returns true - it returns the value of the 2nd, if false - the 3rd.

In lisp any value except for nil(empty list) counts as true.

If you want to evaluate more than one expression in if you use 'do':

( if ( > 2 1 ) ; Is this statement true?
( do
( print "True" )
( print "Also true" ) )
( print "False" ) )
Also true


Here's how you create a 'for' loop:

( for [i ( range 10 ) ]
( print i ) )
0 1 2 3 4 5 6 7 8 9
It allows you to iterate through list or a sequence of numbers.


Learning more

If you want to learn more about hy - you can subscribe to my upcoming tutorials, where I will cover it in more depth.

You can also read hy tutorials and documentation here.

If you have any questions or suggestions - join our community on google plus, or post a thread on the forum.

Another wonderful resource about lisp is a book "ANSI Common Lisp" by Paul Graham, if you seriously want to master lisp - this is one of the best places to start. "Practical Common Lisp" and "The land of lisp" are also great.

Read my best articles:

Inventing Startup Ideas

GTD: The flow state

Learn how to enter the state of flow - state of extreme productivity, complete engagement, and full immersion into what you're doing.

Read More
Creative thinking tools

Thinking Tools for Entrepreneurs

Learn the creative thinking tools you need to use to invent ideas and solve challenges on your path to becoming a great entrepreneur

Read More
Best startup books

Best startup books

This is the collection of my top-favorite best startup books. They're must read for everyone interested in entrepreneurship and business.

Read More
Promote blog and
                                                            drive traffic

Q/A: How can I promote my blog?

Learn the great and very simple strategy to drive traffic to your website. It is the best way to get started promoting your website.

Read More
Want to join our community and post on the forum ? Join Now