diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2021-11-28 13:03:48 -0500 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2021-11-28 13:03:48 -0500 |
commit | 17049953c08b7c783bf73fcb18ddbb7b05c8a671 (patch) | |
tree | 134b4db7e924b4cd803584bef1aef1c0de57d163 /exercism/emacs-lisp/binary/binary.el | |
parent | 65951ec5972aec2abbfc8d0dfa2a7227a0fa7223 (diff) |
add exercises
Diffstat (limited to 'exercism/emacs-lisp/binary/binary.el')
-rw-r--r-- | exercism/emacs-lisp/binary/binary.el | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/exercism/emacs-lisp/binary/binary.el b/exercism/emacs-lisp/binary/binary.el new file mode 100644 index 0000000..2a2fba3 --- /dev/null +++ b/exercism/emacs-lisp/binary/binary.el @@ -0,0 +1,21 @@ +;;; binary.el --- Binary exercise (exercism) + +;;; Commentary: + +;;; Code: + +(require 'cl) + +(defun to-decimal-helper (str) + (cl-loop with result = 0 + for c across str + do (setq result (+ (- c ?0) (* 2 result))) + finally return result)) + +(defun to-decimal (str) + (if (string-match "[01]+" str) + (to-decimal-helper str) + 0)) + +(provide 'binary) +;;; binary.el ends here |