[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
compute ISBN-10, char-to-int?
From: |
Emanuel Berg |
Subject: |
compute ISBN-10, char-to-int? |
Date: |
Thu, 05 Sep 2019 03:32:55 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux) |
Another quality release from
uXu and THE SECRET EMPIRE, this time there sure
was a long distance before the earlier, if
anyone remembers that one... hm... anyway
perhaps _the next one_ will be ISBN-13?
Or wasn't that NASA:s bad luck number?
Typing this in 2017, when the US astronauts,
since abandoning their space shuttle project,
have been reduced to mere *passengers* onboard
Russian Soyuz crafts? You know what I'm saying?
Anyway, questions:
that `char-to-int' looks a bit strange...
?
;;; -*- lexical-binding: t -*-
;; This file: http://user.it.uu.se/~embe8573/emacs-init/isbn-new.el
;; https://dataswamp.org/~incal/emacs-init/isbn-new.el
;; Old ISBN stuff, partially still in use: (?)
;; https://dataswamp.org/~incal/emacs-init/isbn.el
;; NOTE: This isn't a replacement of the old
;; stuff URLd above, this is an all new
;; little project to compute ISBN
;; checksums with Elisp!
;; here is how the ISBN-10 stuff works:
;; https://dataswamp.org/~incal/books/isbn.txt
(require 'cl-lib)
(defun char-to-int (c)
(string-to-number (char-to-string c) ))
;; test:
;; (char-to-int ?0)
(defun checksum-isbn-10 (isbn)
(let*((isbn-list (string-to-list isbn))
(isbn-numbers (remove ?- isbn-list))
(isbn-numbers-9 (cl-subseq isbn-numbers 0 9))
(isbn-ints (cl-map 'list
(lambda (e) (char-to-int e))
isbn-numbers-9) )
(sum 0)
)
(cl-loop for e in isbn-ints
for i downfrom 10
do (cl-incf sum (* e i)) )
(let ((checksum (mod (- 11 (mod sum 11)) 11)))
(if (= 10 checksum) "X" checksum) )))
;; 9 test from [1]:
;;
;; (checksum-isbn-10 "91-7054-940-0") ; 0 (#o0, #x0, ?\C-@)
;; (checksum-isbn-10 "0-201-53992-6") ; 6 (#o6, #x6, ?\C-f)
;; (checksum-isbn-10 "91-85668-01-X") ; "X"
;; (checksum-isbn-10 "91-7089-710-7") ; 7 (#o7, #x7, ?\C-g)
;; (checksum-isbn-10 "9177988515") ; 5 (#o5, #x5, ?\C-e)
;; (checksum-isbn-10 "0312168144") ; 4 (#o4, #x4, ?\C-d)
;; (checksum-isbn-10 "1-4012-0622-0") ; 0 (#o0, #x0, ?\C-@)
;; (checksum-isbn-10 "91-510-6483-9") ; 9 (#o11, #x9, ?\C-i)
;; (checksum-isbn-10 "91-88930-23-8") ; 8 (#o10, #x8, ?\C-h)
;;
;;
;; [1] https://dataswamp.org/~incal/books/books.bib
--
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal
- compute ISBN-10, char-to-int?,
Emanuel Berg <=
- Re: compute ISBN-10, char-to-int?, tomas, 2019/09/05
- Re: compute ISBN-10, char-to-int?, Emanuel Berg, 2019/09/05
- Re: compute ISBN-10, char-to-int?, Eli Zaretskii, 2019/09/05
- Re: compute ISBN-10, char-to-int?, tomas, 2019/09/05
- Re: compute ISBN-10, char-to-int?, Emanuel Berg, 2019/09/05
- Re: compute ISBN-10, char-to-int?, Eli Zaretskii, 2019/09/06
- Re: compute ISBN-10, char-to-int?, tomas, 2019/09/06
- Re: compute ISBN-10, char-to-int?, Emanuel Berg, 2019/09/06
- Re: compute ISBN-10, char-to-int?, Eli Zaretskii, 2019/09/06
- Re: compute ISBN-10, char-to-int?, Stefan Monnier, 2019/09/07