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))