Looking for Programmers

Levi Pearson levi at cold.org
Fri Mar 10 22:31:00 MST 2006

Not looking for a job, but I thought I'd give an example program  
written in Common Lisp for fun.

;;;; A Common Lisp program to find the unique words in a text file that
;;;; are also in a dictionary word list

(defparameter *words-file* "/usr/share/dict/words")

(defparameter *whitespace-chars*
   '(#\Newline #\Space #\Tab #\Page #\Return #\Linefeed))

(defvar *words*
   (with-open-file (words *words-file* :direction :input)
     (let ((hash (make-hash-table :test 'equal)))
       (loop (let ((line (read-line words nil)))
	      (if line
		  (setf (gethash line hash) t)

(defun present-in-hash (key hash)
     (gethash key hash))))

(defun read-word (stream)
   (with-output-to-string (s)
     ;; munch whitespace before
     (loop as c = (peek-char nil stream nil)
        while (and c (member c *whitespace-chars*))
        do (read-char stream))
     ;; copy non-whitespace to output
     (loop as c = (peek-char nil stream nil)
        while (and c (not (member c *whitespace-chars*)))
        do (write-char (read-char stream) s))))

(defun find-unique-words (filename)
   (with-open-file (doc-stream filename :direction :input)
     (let ((word-count (make-hash-table :test 'equal)))
       ;; Count the words
       (loop as w = (string-downcase (read-word doc-stream))
	   while (not (string= w ""))
	   do (when (present-in-hash w *words*)
		(if (present-in-hash w word-count)
		    (incf (gethash w word-count))
		    (setf (gethash w word-count) 1))))
       ;; Collect the unique words
       (loop for word being the hash-key of word-count
	   for count = (gethash word word-count)
	   when (= count 1) collect word))))

More information about the PLUG mailing list