Skip to content

Instantly share code, notes, and snippets.

@larstvei
Last active August 29, 2015 14:05
Show Gist options
  • Save larstvei/08ffc44954860f2b3693 to your computer and use it in GitHub Desktop.
Save larstvei/08ffc44954860f2b3693 to your computer and use it in GitHub Desktop.
(ql:quickload "flexi-streams")
(defvar *words*
(with-open-file (stream "dict" :element-type '(unsigned-byte 8))
(let ((stream (flexi-streams:make-flexi-stream
stream :external-format :utf-8)))
(loop for line = (read-line stream nil 'eof)
until (eq line 'eof) collect
(list (sort (coerce (string-downcase line) 'list) #'char<) line)))))
(defun string-minus (anagram word)
(labels ((iter (anagram word)
(cond ((or (null word) (null anagram)) anagram)
((equal (car anagram) (car word))
(iter (cdr anagram) (cdr word)))
(t (iter anagram (cdr word))))))
(iter anagram word)))
(defun find-anagrams (word &key reuse)
(let ((diff (if reuse #'set-difference #'string-minus))
(word (sort (coerce (string-downcase word) 'list) #'char<)))
(loop for (sorted str) in *words*
unless (funcall diff sorted word) collect str)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment