From 7eb1e664c8d6b0abdc86753fca6f5df280aa3428 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Mon, 22 Nov 2021 07:38:52 -0500 Subject: hamming solution --- exercism/emacs-lisp/hamming/hamming.el | 14 ++++++++++++++ exercism/emacs-lisp/run-exercises.el | 4 ++++ 2 files changed, 18 insertions(+) (limited to 'exercism/emacs-lisp') diff --git a/exercism/emacs-lisp/hamming/hamming.el b/exercism/emacs-lisp/hamming/hamming.el index 1ef5eeb..468ad63 100644 --- a/exercism/emacs-lisp/hamming/hamming.el +++ b/exercism/emacs-lisp/hamming/hamming.el @@ -4,6 +4,20 @@ ;;; Code: +(require 'cl-seq) +(require 'cl-lib) + +(defun kj/sum (lst) + (cl-reduce #'+ lst :initial-value 0)) + +(defun hamming-distance (l1 l2) + (when (not (eql (length l1) (length l2))) + (error 'lengths-differ)) + (let ((fn (lambda (x y) + (if (equal x y) + 0 + 1)))) + (kj/sum (cl-mapcar fn l1 l2)))) (provide 'hamming) ;;; hamming.el ends here diff --git a/exercism/emacs-lisp/run-exercises.el b/exercism/emacs-lisp/run-exercises.el index 44831a6..39372e3 100644 --- a/exercism/emacs-lisp/run-exercises.el +++ b/exercism/emacs-lisp/run-exercises.el @@ -51,4 +51,8 @@ (kj-display "Roman numerals" (to-roman 1990)) +(load-file "hamming/hamming.el") +(kj-display "Hamming distance" + (hamming-distance "GGACGGATTCTG" "AGGACGGATTCT")) + (provide 'hello) -- cgit v1.2.3