Created
October 7, 2011 19:40
-
-
Save fc-unleashed/1271186 to your computer and use it in GitHub Desktop.
Luhn algorithm in elisp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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