Base fun

No Comments

The Lisp reader interprets tokens consisting exclusively of numeric characters as integers. For instance (READ-FROM-STRING "25") returns the integer 25, just as you would expect.

But the dynamic environment can trump even such cast iron expectations. The number radix used by the reader equals the prevailing value of the *READ-BASE* variable. So binding that to 8 (octal) and re-evaluating the function call actually results in 21, not 25.

It gets a little weirder. The Common Lisp reader's radix is permitted to be anything from 2 to 36. With radix 4, for instance, "25" can't be an integer token due to the offending 5 character, so the reader interprets it as the symbol |25|.

In radices exceeding 10, alphabetic characters are used to represent digits weighing more than 9, as A-F are in hexadecimal notation. Base 36 treats the entire alphabet as numeric characters. Well at least according to this expression, my value is 31,201,582:

(LET ((*READ-BASE* 36)) 
(READ-FROM-STRING "Ikram"))

Be the first to write a comment!