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:
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:
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