You're viewing all posts tagged with lisp

A brief explanation of what lisp macros are

A lisp program looks something like this:

(a b c d)
(m o (p k))
(a b c (d e f g (h i j k) l m) 
       n 
       (o p q (r s t (u v) (w x y)) 
           z))

The only form of syntax in lisp is the (a b c ..) form.

The if statement looks like this:

(if a b)

A function call also looks like this:

(foo a b)

A function definition looks sorta like this:

(def foo (a b) body)

Contrast this with python, where the if statement is of the form:

if a: b

And if ‘b’ is multiple lines:

if a:
    b

And the function call is of the form:

foo(a, b)

And the function definition is like:

def foo(a, b):
     body

Without delving too deep into “how”, you can now know what macros do.

Macros are a way of defining new forms of statements.

(bar a b c)

It looks exactly like all other forms of statements.

Why would you define a new form of statements? To abstract the syntax away. To get rid of repetitive code that you can’t get rid of using functions or even higher order functions.

For example:

if some_expression(a, b, c):
      do_something_with( some_expression(a, b, c), d, e, f)
      another_thing(some_expression(a, b, c))

Notice we’re repeating the expression some_expression(a, b, c) and not only that, but we’re calling the function twice.

It’s better to:

x = some_expression(a, b, c)
if x:
    do_something(x, d, e, f)
    another_thing(x)

This is a pattern that can repeat itself in many places.

Some languages allow you to:

if x = some_expression(a, b, c):
   use(x)
   again(x)

But if the language doesn’t provide such a syntax, you can’t define it yourself.

Lisp macros basically allow you to define a new type of statement that 1) look like the rest of the language 2) doesn’t have to wait for the language makers to implement them.

Another good example, the pattern:

try:
    statement block
catch:
    do nothing

In lisp, you can define a macro for that:

(errsafe expression)

Note that an expression can be a statement block.

This is actually the name of an Arc macro, but there’s nothing special about how arc defines it, it’s just like how you can define a function in C; if the standard library doesn’t come with it, you can just write it yourself.

hmm, common lisp? maybe not

Maybe arc?

The thing that’s really attracting me to lisp is the fuss about “macros”.

At first sight, it seems that macros in lisp are necessary because the lisp syntax is very poor and limited.

But, what if this is actually a good thing?

One thing I’m concerned about is, what if my day to day python activity consists of (what would in lisp be) manual expansion of macros? (so called patterns).

I’m not aware of many “patterns” in python, but there are things that I wish can be written more succinctly.

Like, instead of saying:

dict(a=a,b=b,c=c)

why can’t I just say:

m_dict(a,b,c)?

The only time in python where ‘symbols’ are treated as ‘strings’ is keyword arguments.

in dict(a=a), the first a ends up being a string ‘a’, and the second a ends up being the actual a.

Django uses this trick to make querying easy, e.g. query.filter(field__is='value')

From the arc tutorial:

One of the things you’ll discover as you learn more about macros is how much day-to-day coding in other languages consists of manually generating macroexpansions. Conversely, one of the most important elements of learning to think like a Lisp programmer is to cultivate a dissatisfaction with repetitive code. When there are patterns in source code, the response should not be to enshrine them in a list of “best practices,” or to find an IDE that can generate them. Patterns in your code mean you’re doing something wrong. You should write the macro that will generate them and call that instead.

Practical Common Lisp

Reading Paul Graham’s excellent essays, he always keeps blabbing about how great lisp is. This of course makes me kinda interested in wanting to know what this “lisp” thing is, and maybe learn it and start using it.

I’m reading the book “Practical Common Lisp” by Peter Seibel, he also has a tech talk (at google) with the same title.