Touch the firehose of ds106, the most recent flow of content from all of the blogs syndicated into ds106. As of right now, there have been 92792 posts brought in here going back to December 2010. If you want to be part of the flow, first learn more about ds106. Then, if you are truly ready and up to the task of creating web art, sign up and start doing it.

A Few Days With Erlang

Posted by
|

Day 1

Sam:

I started the first day by reading the first half or so of the chapter on Erlang in “Seven Languages in Seven Weeks”. I noted some interesting facts about the language, such as that it powers Facebook’s chat. I think I told every one of my friends about that for the next couple of days, regardless of how little they cared about or understood it.

Matthew executed the example code from the chapter on his own computer while I watched and took notes on what I saw. I started to understand the syntax fairly well after a bit of study, probably a little easier than I picked up on ML in class. I think this was because ML is the most similar to Erlang of all the languages I’ve learned in terms of syntax, so I was more prepared for Erlang by trying ML first. Pattern matching in Erlang is very similar to ML, and the underscore is the wildcard just as in ML.

One thing that I was very interested in was that it is possible to make changes in code while it’s still running. I’d never heard of that being done before, so I was very interested in how it could be done. I found some insight at http://www.erlang.org/course/advanced.html, where it gave me an idea of what is necessary to do this. It wasn’t as thorough as I wanted it to be, as I still have some questions about the process. The “Erlang Whitepaper” at http://www.erlang.org/white_paper.html calls this process “hot code upgrade,” and gave me some more information. It says that “old code can be phased out and replaced by new code,” and that “both old and new code can coexist” while the new code is being added. This is useful for fixing problems in the system without bringing it down. Can other languages do this? When I searched for “hot code upgrade” on Google, everything I saw was about how Erlang can do it. When I searched “hot code update” (thinking it might yield some different results), the first result was on a Scala site that asked “how to steal Erlang’s hot code update for Scala/Java”. This was located at http://scala-programming-language.1934581.n4.nabble.com/how-to-steal-Erlang-s-hot-code-update-for-Scala-Java-td1989938.html. Apparently, most people think it would be extremely difficult to implement in other languages. This goes along with the idea of Erlang making easy things hard, but hard things difficult; the author mentioned this in the chapter (I’m pretty sure a couple of times).

Matthew:

The first day started off pretty well, with lots of promises made in the first few paragraphs.  Easy concurrency?  I was skeptical.  But I trudged along unbiased, trying to take in everything with a clear mind.  Atoms were a little scary for me.  I guess they’re something like labels.  They seem like strings which can’t be broken down into characters.  But then we got to pattern matching.  I’m quite impressed with the notion of matching patterns in tuples, and extracting specific information from structured data.  Functions in Erlang remind me a lot of functions in ML.  I suppose the first thing I noticed about both these languages is that if statements are rare.  Pattern matching is the idea with the most power in Erlang and ML.

Day 2

Sam:

Matthew ran a factorial function that he wrote on his laptop. I became more and more impressed as the numbers grew. The smaller numbers were computed instantly (instantly enough for me, at least), and the computer took less than a minute for insanely large numbers. I remembered writing a Java program in 330 that would calculate a given number of prime numbers; our professor gave us a limit to the number of primes we should request or else we would probably grind Rosemary to a screeching halt while running it. I just thought about that while we were working with the factorials.
We talked about some of the strengths and features of the language. I have not taked CPSC 405 yet, so Matthew explained concurrency and message passing to me. I feel like I got a good idea of what it is and why it is important from his explanation. These things are apparently very fundamental in Erlang.
http://www.erlang.org/doc/reference_manual/functions.html was one of the pages I looked at on this day. One thing really caught my eye, and that was the mention of how “an infinite loop can be done if it uses tail recursive calls”. I thought that, by definition, infinite loops could not be completed; if you use tail recursive calls and complete the loop, it’s not infinite. I thought about that for a while.

Matthew:

When we got the factorial function under our belt, it seemed only fitting that I try to crash it into the ground.  This was the code we used, straight out of the book (excluding the Fibonacci function):

-module(yet_again).
-export([another_factorial/1]).
...
another_factorial(0) -> 1;
another_factorial(N) -> N * another_factorial(N-1).
...

And I played with it.  I ended up taking the factorial of 100,000.  The results are too long to post here, but it didn’t overflow the stack, and took under a minute on my Core i5 processor.  My mind was effectively blown.  Tail recursion is incredibly powerful in Erlang, as the language optimizes tail recursive calls.  The book didn’t talk about how, so I Googled it.  Admittedly, I took the following explanation from answers.com, or something like that.  When Erlang makes a recursive call at the end of a function, instead of adding another frame to the stack with new data in it, Erlang replaces the current frame with the frame for the new call using the data from the parent call.  This allows an infinite number of tail recursive calls without any worry of stack overflow.  Awesome.

Day 3

Sam:

Matthew wrote a recursive function that would take a string and return the number of words in it. That was one of the exercises presented in the book. I thought about it myself but was unable to come up with a good solution, so Matthew took the time to explain the code to me. I was also very impressed by this function. We gave it exceptionally large strings (copypasted lorem ipsum), and as on the second day, it gave us results in no time. I’ve never worked with a language that could do so much so quickly.
I looked at http://www.erlang.org/faq/faq.html. This page has a bunch of information on how to do different things in Erlang, frequently asked questions about the language (which I found to be quite informative), applications that Erlang is especially useful for (including telecommunication systems and applications, servers for internet applications, and database applications that require soft realtime behavior), and a lot of other interesting information. It also includes information about troubleshooting, tools for using the language, and Erlang libraries.
I think I’ve been sold on Erlang. It’s taking me a while to get used to functional languages, but I am much more comfortable now. Also, the speed of the language is quite impressive.

Matthew:

One of the more powerful examples of pattern matching we discovered by doing an exercise in the book.  The exercise asked us to create a function which returned the word count of a string.  It is important to note that strings are simply an implementation of lists, and that every character maps to an integer in Erlang (” ” = [32]).  Here’s the code:

words([]) -> 0; %% Empty strings have no words!
words([32]) -> 0; %% A space by itself is not a word.
words([_]) -> 1; %% A letter by itself is a word.
                 %% Combine with logic below to determine that
                 %% if the last character of a string is not a space,
                 %% it is ending a word.
words([32 | Tail]) -> words(Tail); %% A word does not start with a space
words([_, 32|Tail]) -> 1 + words(Tail); %% This would indicate that one word
                                        %% has ended, and is not the end of
                                        %% a string.
words([_ | Tail]) -> words(Tail). %% This would indicate that we are at the
                                  %% beginning or in the middle of a word.

This code quickly chewed through a 1000-word “Ipsum Lorem”.  Again, I don’t have space to post the results here, but my mind was blown.  Again.  I’m very impressed with Erlang, although the “Day 3″ portion in the book started introducing some wild modules which were challenging at best to understand.  I couldn’t tell if they were conventions or custom code the author had made.  I was quite confused.

I think my most favorite part of Erlang is the notion of List Comprehensions–a way to “build” lists, much like how one can build sets in abstract algebra.  In set theory, one can construct the set of all even positive integers less than 10 with the following:

S = {2x | x \in N, x < 10}

The ‘N’ represents the set of all natural numbers (or positive integers), and ‘\in’ is the LaTeX code for “is a member of”.  In Erlang, this is equivalent to:

S = [X * 2 | X <- [1, 2, 3, 4, 5], 2 * X < 10].

Erlang definitely has some roots in abstract algebra, and, as a mathematician, I love it.

Add a comment

ds106 in[SPIRE]