I find very strange that the ANSI specification defines the function DIGIT-CHAR, but doesn't also define CHAR-DIGIT. It would be nice to have it in alexandria.<br><br> One can use char-code to implement it, but only assuming that the char-code of alphabetic characters are sequential (which happens very oftenly, I don't know any implementation in which this may not work, but this behaviour is not required by the specification).<br>
<br>So, here is my suggestion:<br><br><span style="font-family: courier new,monospace;">(defun char-digit (char &optional (radix 10))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> "Takes a character and returns its weight.</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> Signals an error if char is not a digit character in the given radix."</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (assert (digit-char-p char radix) (char) "Character ~a is not a digit character." char)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (let ((char (char-upcase char)))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> #+(or sbcl clisp ecl cmucl openmcl</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> allegro lispworks corman gcl abcl unicode)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (cond</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> ((char<= #\0 char #\9)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (- (char-code char) (char-code #\0)))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> ((char<= #\A char #\Z)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (+ 10 (- (char-code char) (char-code #\A))))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (t nil))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> #-(or sbcl clisp ecl cmucl openmcl</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> allegro lispworks corman gcl abcl unicode)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (parse-integer (string char) :radix radix)))</span><br>
<br>I tested and it works. Well, I am open to suggestions, and feel free to include this code in alexandria as well.<br><br>Gustavo.<br>