Archive for October, 2007

Verhoeff Check Digit Algorithm in Erlang

Monday, October 29th, 2007

If you are going to implement a Check Digit algorythm for a nwe Product, don’t stay with some inferior thing like the EAN Check Digit. Seemingly the Verhoeff algorithm is by far superior in catching common data entry errors happening to humans.

Let’s calculate the Check Digit for a String in Erlang:

-define(DIHEDRAL_A, {{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
                     { 1, 2, 3, 4, 0, 6, 7, 8, 9, 5 },
                     { 2, 3, 4, 0, 1, 7, 8, 9, 5, 6 },
                     { 3, 4, 0, 1, 2, 8, 9, 5, 6, 7 },
                     { 4, 0, 1, 2, 3, 9, 5, 6, 7, 8 },
                     { 5, 9, 8, 7, 6, 0, 4, 3, 2, 1 },
                     { 6, 5, 9, 8, 7, 1, 0, 4, 3, 2 },
                     { 7, 6, 5, 9, 8, 2, 1, 0, 4, 3 },
                     { 8, 7, 6, 5, 9, 3, 2, 1, 0, 4 },
                     { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }}).
-define(DIHEDRAL_INVERSE, {0, 4, 3, 2, 1, 5, 6, 7, 8, 9}).
-define(DIHEDRAL_P, {{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
                     { 1, 5, 7, 6, 2, 8, 3, 0, 9, 4 },
                     { 5, 8, 0, 3, 7, 9, 6, 1, 4, 2 },
                     { 8, 9, 1, 6, 0, 4, 3, 5, 2, 7 },
                     { 9, 4, 5, 3, 1, 2, 6, 8, 7, 0 },
                     { 4, 2, 8, 6, 5, 7, 3, 9, 0, 1 },
                     { 2, 7, 9, 3, 8, 0, 6, 4, 1, 5 },
                     { 7, 0, 4, 6, 9, 1, 3, 2, 5, 8 }}).

verhoeff_digit_helper(Char, {Pos, X}) ->
    Pval = element((Char-$0)+1,
                    element(((Pos+1) rem 8)+1, ?DIHEDRAL_P)),
    XNew = element(Pval+1, element(X+1, ?DIHEDRAL_A)),
    {Pos + 1, XNew}.

verhoeff_digit(NumStr) ->
    {_, X} = lists:foldl(fun(Char, Acc) ->
                             verhoeff_digit_helper(Char, Acc)
                         end,
                        {0, 0}, lists:reverse(NumStr)),
    [$0 + element(X+1, ?DIHEDRAL_INVERSE)].

[$9] = verhoeff_digit("123456654321").

Unfortunately there are various (contradicting) implementation guidelines for the Verhoeff algorithm. I followed Perl’s Algorithm::Verhoeff, Jonathan Mohr and Wikipedia.

Calculating the EAN Check Digit in Erlang

Monday, October 29th, 2007

EAN and UPC codes come with a Check Digit in the end. A tool for EAN Check Digit calculation (called EANcalc) was the first program I ever sold. It was written in GW-Basic and I sold it for 30 DM (15 U$). I was 12 or so.

Since then I have implemented EAN Check digits in many Languages. Now in Erlang:

% @doc calculate a EAN check digit
ean_digit(Nums) ->
    {_, Summe} =
    lists:foldr(fun(X, {Factor, Summe}) ->
                    {4-Factor, Summe + ((X-$0) * Factor)}
                end, {3, 0}, Nums),
    [$0 + ((10-(Summe rem 10)) rem 10)].

make_ean(Num) ->
    Prefix = "4005998",
    EanRaw = Prefix ++ Num),
    EanRaw ++ ean_digit(EanRaw).

[$2] = ean_digit("400599871650")
"4005998716502" = make_ean("71650")

Saturday, October 27th, 2007

Erlang Debugger

Friday, October 19th, 2007

One thing I really like about Erlang is the Debugger. Nothing breathtaking or hyper-innovative there but it get’s the job done. that’s no little feat for a relatively obscure language.

ActiveMQ Software Quality

Wednesday, October 17th, 2007

Sometimes I was wondering I I have been to harsh with ActiveMQ. Companies are actually using it. It can’t be that bad – can it. Now I came acress this status screen and feel firmer in my assessment:

-52 Messages in a Queue? WTF?

Erlang Compiler

Monday, October 8th, 2007
[md@boingball ~/kernelE]$ gmake regression
erlc +debug_info -DEUNIT -DLOG_DEBUG -Wall  -o ebin src/mypl_db.erl
src/mypl_db.erl:none: error in parse transform 'eunit_autoexport': {undef,
                                              [{eunit_autoexport,
                                                parse_transform,
                                                ...
[4000 lines left out]