Files
exercism/elisp/anagram/anagram.el
T

44 lines
1.5 KiB
EmacsLisp
Raw Normal View History

2019-01-20 05:38:02 +00:00
;;; anagram.el --- Anagram (exercism)
;;; Commentary:
;;; Code:
(provide 'anagram)
2019-01-20 19:37:55 +00:00
(defun get-char-count(w)
"Return an associative list of character counts for W."
(let ((char-list (mapcar (lambda (char) (string char)) w))
(char-count ()))
2019-01-20 05:38:02 +00:00
;; Build an associative list from the character list.
2019-01-20 19:37:55 +00:00
(dolist (char char-list char-count)
(if (assoc char char-count)
(setcdr (assoc char char-count)
(1+ (cdr (assoc char char-count))))
(setq char-count (cons (cons char 1) char-count))))))
2019-01-20 05:38:02 +00:00
(defun compare-char-counts(char-counts comparison-char-counts)
(let ((are-equal t))
(dolist (char-count char-counts are-equal)
(if (not are-equal) nil)
(if (assoc (car char-count) comparison-char-counts)
(if (not (equal
(cdr char-count)
(cdr (assoc (car char-count) comparison-char-counts))))
(setq are-equal nil))
(setq are-equal nil)))))
(defun anagrams-for(word anagrams)
(let ((candidates ())
(word-char-count (make-char-count word)))
(dolist (candidate anagrams candidates)
(if (and
(not (string-equal word candidate))
(equal (length word) (length candidate))
(compare-char-counts word-char-count (make-char-count candidate)))
(if (> (length candidates) 0)
(add-to-list 'candidates candidate t)
(setq candidates (cons candidate nil)))))))
;;; anagram.el ends here