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)

  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

 (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

 (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

 (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

 (save-tdf-info "tdf.dat") (restore-tdf-info "tdf.dat")

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

Kata 6 In Lisp

I got bored tonight and had a go at writing Dave Thomas’ Kata 6 in Lisp. It just seemed like a good thing to do. The code is below. I’m not a Lisp wizard by any stretch, so I welcome any comments from Lisp mavens. It’s interesting to note that this version comes up with 2,531 matches, while my Ruby version only found 2,506. Dave says you should find 2,530. Also note that all I did was the finding. I didn’t implement the largest set, long word, etc from the original kata.

(setq anagrams (make-hash-table)) 
(setq count 0) 
(defun canon (word)   
(setq norm-word (string-downcase word))  
 (setq canon-word (sort (copy-seq norm-word) #'char-lessp))  
 (setq canon-word (intern canon-word))  
 (setf (gethash canon-word anagrams)        
 (cons norm-word              
 (gethash canon-word anagrams))))  
(with-open-file (stream "wordlist.txt")                 
(do ((line (read-line stream nil)                            
(read-line stream nil)))                   
  ((null line))
                   (setq count (+ count 1))
                   (canon line)))
  (maphash #'(lambda (key val)
              (if (= (length val) 1)
                  (remhash key anagrams)))
          anagrams)
  (format t "Total words: ~D; Total anagrams: ~D" count
         (hash-table-count anagrams))
  (maphash #'(lambda (key val)
              (print val))
          anagrams)

Update: I discovered today that instead of interning the string I could have created the hashtable with a different test, like so

 (setq anagrams (make-hash-table :test #'equal)) 

and then removed this line

 (setq canon-word (intern canon-word))