Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Luhn algorithm in elisp
(defun luhn-sum (list n)
(if (null list)
0
(+ (let ((x (car list)))
(if (= 1 (mod n 2))
(let ((y (* 2 x)))
(if (> y 9)
(+ 1 (mod y 10))
y))
x))
(luhn-sum (cdr list) (+ 1 n)))
)
)
(defun card-no-str-to-num (card-no)
(mapcar (lambda (x) (string-to-number x 10)) (cdr (reverse (cdr (split-string card-no "")))))
)
(defun luhn-check (card-no)
(eq 0 (mod (luhn-sum (card-no-str-to-num card-no) 0) 10))
)
(luhn-check "49927398716")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment