Erlang Radix Syntax, and Lambda as the Ultimate Meaning

Follow Up to “Gotcha with Bitstrings”  

   How to ACTUALLY denote binary numbers in Erlang

After I put the previous post together, I went looking if there was a way for the Erlang lexer to tell if a number is binary, or octal, or hex.  Well it turns out: yes, and it is a beautiful bit of syntax.  
All you do is put the radix (a.k.a. the base) in front of the number and then use the pound-sign ‘#’ to separate them:

As you can see, the literal 2#01000001 is stating:  “use base-2, and treat the numeral after the pound-sign as binary”. The syntax  <<2#01000001>> on line 128 is bitstring syntax and was the subject of the last post. The evaluation of a naked number inside the chevrons shoves that number into a 8-bit least-significant segment which, in this case, does not truncate any bits. The 65 decimal number is interpreted as the literal UTF-8/ASCII value of "A". They are the same.

If we try to use a non-binary glyph/symbol (like a ‘2’) for instance, we will get a problem:
We can do hex too, by using 16 as the radix:

This little bit of syntax is allowed for radices up to 36 (presumably because nobody would agree as to what new glyphs/symbols we should use after we have exhausted the Indian Numerals and the Latin Basic Alphabet).

If we ever do try to adopt extra glyphs for a > base-36 system.  I suggest the one above (hereafter known as the Eklund Encoding), as it makes lambda the answer to life, the universe, and everything.

Although for generating tiny URLs from UUIDs without accidentally offending someone, I suggest the crockford encoding.

No comments:

Post a Comment