Single

Lisp Macros Are Very Cool

So I’m playing around with Lisp, reading Successful Lisp and thoroughly enjoying myself. I really like Lisp, I just haven’t gotten to use it on anything other than test stuff yet. One of the things that I find the most interesting, and powerful, is the macro facility. Sure, some languages like C have macros that are processed by a preprocessor, but Lisp’s macros are in a league of their own. Consider this code (lifted wholesale from Successful Lisp)

class="highlight">
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1  (defmacro def-i/o (writer-name reader-name (&rest vars)) 
      2    (let ((file-name (gensym)) 
      3          (var (gensym)) 
      4          (stream (gensym))) 
      5      `(progn 
      6         (defun ,writer-name (,file-name) 
      7           (with-open-file (,stream ,file-name 
      8                                    :direction :output 
      9                                    :if-exists :supersede) 
     10                           (dolist (,var (list ,@vars))
     11                             (declare (special ,@vars))
     12                             (print ,var ,stream)))) 
     13
     14         (defun ,reader-name (,file-name) 
     15           (with-open-file (,stream ,file-name
     16                                    :direction :input
     17                                    :if-does-not-exist :error) 
     18                           (dolist (,var ',vars) 
     19                             (set ,var (read ,stream))))) 
     20         t)))

What does this mass of parentheses, backquotes, commas and colons do? Lots. Executing the macro thusly

class="highlight">
1
(def-i/o save-checks load-checks (*checks* *next-check-number* *payees*))

will define two functions, one called save-checks and the other called load-checks, that will store and retrieve the global variables *checks*, *next-check-number* and *payees* to and from a given file name. These methods could be called thusly

class="highlight">
1
(save-checks "checks.dat") (load-checks "checks.dat")

This macro could be included in any program for which we needed to have reader and writer functions for marshaling data to and from disk files. This example was for a fictional bank, but let’s say I had a program to process data about the Tour de France and I had buckets for teams, riders, jerseys and sponsors. I could do this

class="highlight">
1
(def-i/o save-tdf-info restore-tdf-info (*riders* *teams* *jersyes* *sponsors*)

and would get save-tdf-info and restore-tdf-info functions that could be called thusly

class="highlight">
1
(save-tdf-info "tdf.dat") (restore-tdf-info "tdf.dat")

Maybe I’m just easily impressed, but I think that’s pretty cool.

This post is licensed under CC BY 4.0 by the author.