isbn.erl: My first Erlang module

For a few weeks now I’ve been tinkering with, learning about, and falling in love with Erlang. I’ve been noticing the buzz about Erlang over the past few months, but two things won me over: the Erlang video and how amazingly simple and elegant concurrency and message passing is.

For the past few weeks I’ve been reading the Erlang documentation, Joe Armstrong’s new book, reading the trapexit wiki, and lurking in #erlang on irc.freenode.net. If you’re wading in the erlang waters, I highly suggest just about every link in this wonderful roundup of beginners erlang links.

After a few weeks of reading and tinkering in the shell I decided that it was time to come up with a quick project to hone my Erlang skills. I had tinkered with ISBN validation and conversion while writing a small django application to catalog the books on the bookshelf, so I thought that was a good place to start. The Wikipedia page and my collection of ISBN links provided me with more than enough guidance on validation, check digit generation, and conversion.

I found it very easy to build this module from the ground up: start with ISBN-10 check digit generation, then use that to build an ISBN-10 validator. I did a similar thing with ISBN-13, writing the check digit generator and then the ISBN-13 validator. From there I was able to build on all four public functions to write an ISBN-10 to ISBN-13 converter as well as an ISBN-13 to ISBN-10 converter (when that is a possibility). In the process of this very simple module I ended up learning about and applying accumulators, guards, the use of case, and lots of general Erlang knowledge.

Here’s a peek at how the module works. After downloading it via google code or checking out the latest version via Subversion, the module needs to be compiled. This is easily accomplished from the Erlang shell (once you have Erlang installed of course):

mcroydon$ erl
Erlang (BEAM) emulator version 5.5.4 [source] [async-threads:0] [kernel-poll:false]

Eshell V5.5.4  (abort with ^G)
1> c('isbn.erl').
{ok,isbn}

After that we can use any of the exported functions:

2> isbn:validate_13([9,7,8,1,9,3,4,3,5,6,0,0,5]).
true

Let’s trace the execution of a simple function, check_digit_10/1. The function expects a list of 9 numbers (the first 9 numbers of an ISBN-10) and returns the check digit as either an integer or the character 'X'. The first thing that the function does is check to see if we’ve actually passed it a 9-item list:

check_digit_10(Isbn) when length(Isbn) /= 9 ->
    throw(wrongLength);

This is accomplished with a simple guard (when length(Isbn) /- 9). If that guard isn’t triggered we move on to the next function:

check_digit_10(Isbn) ->
    check_digit_10(Isbn, 0).

This forwards our list of 9 numbers on to check_digit_10/2 (a function with the same name that takes two arguments. We’ll see in a minute that the 0 I’m passing in will be used as an accumulator. The next function does most of the heavy lifting for us:

check_digit_10([H|T], Total) ->
    check_digit_10(T, Total + (H * (length(T) + 2)));

This function takes the list, splits it in to the first item (H) and the rest of the list (T). We add to the total as specified in ISBN-10 and then call check_digit_10/2 again with the rest of the list. This tail-recursive approach seems odd at first if you’re coming from most any object oriented language, but Erlang’s function nature, strong list handling, and tail-recursive ways feel natural very quickly. After we’ve recursed through the entire list, it’s time to return the check digit (or 11 minus the total modulus 11). There’s a special case if we get a result of 10 to use the character ‘X’ instead:

check_digit_10([], Total) when 11 - (Total rem 11) =:= 10 ->
    'X';

Finally we return the result for the common case given an empty list:

check_digit_10([], Total) ->
    11 - (Total rem 11).

Feel free to browse around the rest of the module and use it if you feel it might be useful to you. I’ve posted the full source to my isbn module at my isbnerl Google Code project, including the module itself and the module’s EDoc documentation. It is released under the new BSD license in hopes that it might be useful to you.

I learned quite a bit creating this simple module, and if you’re learning Erlang I suggest you pick something not too big, not too small, and something that you are interested in to get your feet wet. Now that I have the basics of sequential Erlang programming down, I think the next step is to make the same move from tinkering to doing something useful with concurrent Erlang.

8 Responses to “isbn.erl: My first Erlang module”


  1. 1 Jeremy May 4th, 2007 at 11:08 am

    Great stuff. :-)

    I was surprised and rather pleased to see my own first dabblings in Erlang on the list of links you linked to.

  2. 2 Mike McNally May 4th, 2007 at 11:18 am

    Calling “length()” on each iteration isn’t necessary. You know the length at the start, so you can just pass forward a length with your iteration accumulator.

  3. 3 Mike McNally May 4th, 2007 at 11:25 am

    Also, instead of the check digit special case being a separate function body, I’ve grown to prefer using a “case” expression, perhaps like this:

    check_digit_10([], T) ->
    case T rem 11 of
    1 -> ‘X’;
    N -> 11 - N
    end.

    It’s a matter of personal preference of course.

  4. 4 Bill Compton Jun 4th, 2007 at 2:27 pm

    Hi Jim. Photos i received. Thanks

  5. 5 vmqoegiacy Jul 31st, 2007 at 11:32 pm

    Hello! Good Site! Thanks you! qzmgyvrrvz

  6. 6 htcfxrcygz Nov 25th, 2007 at 10:01 pm

    The Government’s examinations watchdog used the hotels to host meetings in the course of a recent review it was carrying out into the secondary school curriculum. advertisement The full extent of the spending only emerged after a whistleblower within the organisation raised concerns. Matthew Elliott, the chief executive of the Taxpayers’ Alliance, said: “The QCA’s focus should be on improving the qualifications and exams for children in this country, not living the high life at taxpayers’ expense. “If their offices are not large enough for a conference and they need outside accommodation, they should pick cheaper locations rather than frittering away other people’s hard earned cash. “The QCA should show a bit more humility and respect for…http://brendsite.com/

  1. 1 sirf iii gps receiver Trackback on Feb 12th, 2008 at 1:26 pm
  2. 2 freedom keychain gps Trackback on Feb 15th, 2008 at 4:46 pm

Leave a Reply