isbn.erl: My first Erlang module


Posted

in

,

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.

Comments

63 responses to “isbn.erl: My first Erlang module”

  1. Lady Gaga Avatar

    I tried to get the feed for the RSS for this webpage & for some odd reason it isn’t displaying in Google Chrome. Does anyone have any ideas???

  2. Beyonce Avatar

    I am a tremendous fan of your weblog! Appear at my site, You’ll be able to check your web page right here!

  3. Justin Bieber Avatar

    I used to be pretty delighted to find out this internet sites on bing.I wished to say a great number of due to you with regard to this outstanding piece!! I surely liked every last small little bit of it and I’ve you bookmarked to have a glimpse at new things you submit.

  4. Pioneer vsx-1020-k review Avatar

    Unusual this submit is totaly unrelated to what I was searching google for, nevertheless it was listed at the first page. I guess your doing something proper if Google likes you sufficient to put you at the first page of a non comparable search.

  5. PPI Information Avatar

    this is nice information need to know more

  6. Lady Gaga Avatar

    Considerably, the page is actually the sweetest on this noteworthy topic. I harmonise with your conclusions and will thirstily appear forward to your approaching updates. Just saying thanks won’t just be sufficient, for the amazing clarity inside your writing. I will immediately grab your rss feed to remain informed of any updates.!

  7. Lady Gaga Avatar

    I realistically like your web log material the way you place up the things…I’ve peruse the matter with wonderful interest.

  8. jammers for sale Avatar

    I image this may well be various upon the written content? even so I still consider that it may be suitable for just about any type of topic subject material, because it might incessantly be pleasurable to resolve a heat and delightful face or maybe listen a voice while preliminary landing.

  9. zero friction marketing Avatar

    You completed certain good points there. I did a search on the theme and found nearly all people will consent with your blog.

  10. discount tory burch shoes…

    Tory Burch Handbags is a trendy yet durable.Tory Burch is an American fashion designer who got her start on the upper-east side of Manhattan. Tory Burch Designer Handbags are a great collection of high quality fun handbags available in a wide variety o…

  11. cheap tory burch shoes …

    As the summer goes, winter comes.You should have some shoes in store. Based on this point, the cheap tory burch shoes Shoes is developing faster and faster. More and more people do online shopping.With so many shoes in it, now I advise you getting a …

  12. web hosting in delhi Avatar

    Thanks for making the sincere attempt to talk about this. I think very strong about it and wish to read more. If it’s OK, as you acquire extra intensive wisdom, would you thoughts including extra articles very similar to this one with more information? It might be extraordinarily useful and useful for me and my friends.

  13. CrosbyCasandra Avatar

    I took my first personal loans when I was 25 and that aided my relatives a lot. Nevertheless, I need the consolidation loans also.