Thinking in a Lispy way

In the previous article, Lisp for C programmers, I wrote about how, if you're familiar with C, you can program in Lisp using similar constructs, just making adjustments for the Lisp brackets and the differences in the names of the language elements.

However, to really take advantage of Lisp's features you need to think in a slightly different, Lispy, way. Here I'll introduce three aspects of Lisp that can really make it easier to express your ideas in a project, with examples that typically occur when programming for a microcontroller board:

  • Using lists to represent multiple values.
  • Writing anonymous functions.
  • Writing a function that returns a function.

I've kept the symbol names short so you can run these examples on any platform, even an Arduino Uno.

This originally appeared as a post on the uLisp forum.

The C way of doing things

Here's the beginning of a typical Arduino program; suppose we've got digital I/O pins 2, 4, and 7 connected to LEDs. Then to light up the middle LED we need to do:

pinMode(2, OUTPUT);
pinMode(4, OUTPUT);
pinMode(7, OUTPUT);
digitalWrite(2, LOW);
digitalWrite(4, HIGH);
digitalWrite(7, LOW);

The direct equivalent in uLisp is:

(pinmode 2 t)
(pinmode 4 t)
(pinmode 7 t)
(digitalwrite 2 0)
(digitalwrite 4 1)
(digitalwrite 7 0)

Using lists to represent multiple values

However, in Lisp we can eliminate a lot of this repetition by representing the LED I/O pins as a list:

(2 4 7)

uLisp includes several functions to handle lists; for example mapc performs a function on every element of one or two lists. So to define the three pins as outputs we could write:

(mapc pinmode '(2 4 7) '(t t t))

This calls pinmode with the first elements of (2 4 7) and (t t t), then the second elements of (2 4 7) and (t t t), and so on (until the shortest list runs out). Likewise, to set the outputs we can do:

(mapc digitalwrite '(2 4 7) '(0 1 0))

Writing anonymous functions

We could write a function out that takes a single argument, the pin number, and configures that pin as an output:

(defun out (pin) (pinmode pin t))

Then we could supply out to mapc to define the three pins as outputs more elegantly:

(mapc out '(2 4 7))

What we've just done is to define a function out just so we could use it as the argument to the mapping function, mapc.

Even better, Lisp allows you to define a function in situ, without having to define it earlier with defun. This is called an anonymous function. This is especially useful if you only want to use the function in one place. The construct to do this is called lambda; you write:

(mapc (lambda (pin) (pinmode pin t)) '(2 4 7))

The lambda specifies the same body of the function without having to define it earlier.

Writing a function that returns a function

Suppose the three pins 2, 4, and 7 are connected to an RGB LED, and we want to work with this without having to remember which pins it is connected to. We could write a function led which takes a list of the pins, pns,  that connect to the LED, and returns a function of three arguments, again using lambda, to write the colour states to those pins:

(defun led (pns)
  (lambda (r g b)
    (mapc digitalwrite pns (list r g b))))

We can use it like this:

(defvar rgb (led '(2 4 7)))

This creates a function rgb that will write to the RGB LED. To set the LED to magenta we can write:

(rgb 1 0 1)

If we had a second RGB LED connected to pins 8, 12, and 13 we could write:

(defvar r2 (led '(8 12 13)))

and make this LED cyan by writing:

(r2 0 1 1)

We can now use the functions rgb and r2 without having to remember which pins the LEDs are connected to.


These are a few simple examples demonstrating how you can do things with Lisp that make it easier to create practical applications with less programming effort.