Created
September 14, 2011 00:07
-
-
Save arademaker/1215526 to your computer and use it in GitHub Desktop.
Verifying ISSN's Check digit using Lisp
This file contains hidden or 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 check-issn (str) | |
" Parse a string of 8 digits in the format NNNN-NNNN representing an | |
ISSN number. The last N can be an X representing 10. " | |
(let ((digits (remove nil (loop for char across str | |
collect (cond ((equal char #\X) 10) | |
((equal char #\-) nil) | |
(t (parse-integer (string char)))))))) | |
(if (not (equal (length digits) 8)) | |
nil | |
(equal 0 (mod (loop for i from 8 downto 1 | |
for j in digits | |
summing (* j i)) 11))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment