Refactor elisp/anagram/anagrams-for

This commit is contained in:
dwrz
2019-01-20 19:38:32 +00:00
parent a2bca632e8
commit ca9c5f7d51
+4 -2
View File
@@ -31,13 +31,15 @@ CHAR-COUNT and COMPARISON-CHAR-COUNT should be alists in (character . count) for
(setq are-equal nil))))) (setq are-equal nil)))))
(defun anagrams-for(word anagrams) (defun anagrams-for(word anagrams)
"Return all elements of ANAGRAMS which are anagrams for WORD.
Case insensitive."
(let ((candidates ()) (let ((candidates ())
(word-char-count (make-char-count word))) (word-char-count (get-char-count (downcase word))))
(dolist (candidate anagrams candidates) (dolist (candidate anagrams candidates)
(if (and (if (and
(not (string-equal word candidate)) (not (string-equal word candidate))
(equal (length word) (length candidate)) (equal (length word) (length candidate))
(compare-char-counts word-char-count (make-char-count candidate))) (equal-char-count word-char-count (get-char-count (downcase candidate))))
(if (> (length candidates) 0) (if (> (length candidates) 0)
(add-to-list 'candidates candidate t) (add-to-list 'candidates candidate t)
(setq candidates (cons candidate nil))))))) (setq candidates (cons candidate nil)))))))