Skip to content

Instantly share code, notes, and snippets.

@fc-unleashed
Created October 7, 2011 19:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fc-unleashed/1271186 to your computer and use it in GitHub Desktop.
Save fc-unleashed/1271186 to your computer and use it in GitHub Desktop.
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