It’s just data

Erlang: First Impressions

In Ruby, constants start with a capital letter.  In Erlang, they do too.

In Ruby, symbols start with a colon, and are typically followed by a lower case letter.  Symbols are heavily used in Ruby programs.  In Erlang atoms serve a similar purpose, and start with a lower case letter (without a sigil).  Atoms are heavily used in Erlang programs too.  Single quotes may be used to create atoms that aren’t simple identifiers.  Note that atoms which contain @ and . characters do not need to be quoted, among other things this facilitates naming things that may exist on other machines.

In Ruby, method names start with a lower case letter, and class names start with an upper case letter.  In Erlang, function names start with a lower case, and namespace names also start with a lower case letter.

In Ruby, variable names start with a lower case letter.  Erlang doesn’t have variables.  Instead, every process has a hash that you can access via built in functions, get and put.  These functions aren’t as heavily used as you might think.

Infix operators (like +) are functions in Erlang as oposed to polymorphic methods.  + add two numbers, ++ concatenates two lists.  Strings are lists in Erlang.

Like most languages these days, Erlang supports a JSON like syntax for data structures, with a few notable differences.  For starters, Erlang doesn’t have a native syntax for hash or dictionary, and does have Python-esque distinction between tuples (which are immutable) and lists (which are optimized for “head” and “tail” operations).  There is a concept of a record, which is closer to a C style “struct” than a Ruby style hash.  In any case, heavy use of atoms lead to a different style of making data structures self documenting, for example a hypertext link modeled in Erlang might look like this:

{a, [{href, "http://example.com"}]}

This is a tuple, containing an atom and a list.  This list contains a single tuple, which contains an atom and a string.

Such data structures are immutable, and can’t contain any variables or pointers.  That makes them inherently serializable, which Erlang does transparently when it passes message between processes.

Comments start with a % instead of a #.

Statements end with a period followed by whitespace.  Statements may have many clauses, separated by semicolons.  Clauses may define a number of data constructors, separated by commas.  Like in Ruby methods, the last data constructor is the return value for a function.

Functions in Erlang are a clause.  Here’s a simple one:

square(X) -> X*X.

Erlang programs don’t tend to make heavy use of traditional control structures.  Instead they tend to rely on QBE style pattern matching and recursion.  Here’s an example:

area({circle, R}) -> PI * R *. R;
area({triangle, Base, Height}) -> 0.5 *. Base *. Height.

The above defined a single function, named area/1 (the slash one indicating one argument).  When called with a tuple of length two, and with the first element matching the atom circle, R is first bound to the value of the second element, then the remainder of the clause is evaluated.  If the arguments don’t match, the next patterns is checked.  If none match, an error is generated.

In some cases, it is helpful to use pattern matching variables which start with an underscore: these essentially mean “don’t care”.

Another example:

shampoo(HAIR) when not clean(HAIR) ->
  lather(HAIR),
  rince(HAIR),
  shampoo(HAIR);
shampoo(HAIR) -> HAIR.

This fictitious example demonstrates both gates that can’t be expressed by simple pattern matching, and tail recursion.

Erlang does have if and case statements, and they too rely heavily on the pattern matching idiom.  It is worth nothing that pattern matching in Erlang is simply an alternate way to express if statements, and are done sequentially.  No XSLT style weighting heuristics nor Prolog style backtracking is performed.

At the top of source files you will typically find a series of declarations which start with a -.  This fills the role of C # pragmas, and may be used to define the name of the module, list what is exported, include other files, and the like.

Erlang has a number of built in functions (BIFs to use the lingo) which do various things like map and filter.  The nearest equivalent of lambda is funspawn is used to create either local or remote processes.  Local processes are essentially green threads, though I suspect that there is heavy use of synchronous  I/O multiplexing going on behind the scenes.

Another BIF is c, which compiles Erlang code

Sending a message to another process is achieved via the ! operator.  receive is a statement built into the language that blocks the process until data is available, but otherwise is identical to a pattern matching case statement.  Analogous to C's idiom of having a break statement separating clauses in a switch statement, a common idiom in Elang is to terminate each receive clause with a tail recursion call back to the function which contains the receive statement.  After seeing this a few times, one quickly starts thinking of this as a form of goto instead of as a mind-bending form of recursion.  This isn’t as bad as it sounds.

The above should be enough to make the concurrency section of the Getting Started manual comprehensible.

Once that is mastered, one can move onto to Yaws, ErlyWeb and Mnesia (overview).


It seems to me that I only find a perfect summary for a language only when I’m done reading the entire manual. Happened with Ruby, and now again with Erlang.

Thanks Sam. I appreciate it.

Posted by Rami Kayyali at

So, not having used Erlang enough (maybe 15-30mins total?) to really say how good this post is (although I think the presentation is pretty clear), what do you think of Erlang?  Do you think it’ll be big the way many Erlang advocates believe it will? See this article for one of the most recent such posts I’ve seen on the topic.

Posted by roberthahn at

what do you think of Erlang?

I’m still attempting to digest the language.  Posting this was a part of that process.

A platform designed from the ground up to support a “distributed DataBase Management System, appropriate for telecommunications applications and other ... applications with need of continuous operation and soft real-time properties” is of interest to me.

Posted by Sam Ruby at

Nice summary of the syntax.

I’ve been coding a lot of Erlang lately and I’m very impressed with it. I actually have a lot of experience with pattern matching functional languages like SML and Haskell though so that was a bit of a leg up on learning it.

Mnesia and OTP in particular I’m really excited about. Mnesia is fast and makes replication and horizontal scaling look like child’s play. Using list comprehensions as the query language is just way too cool. OTP kind of strikes me as Rails for distributed systems, taking all the patterns and best practices that experienced developers have settled on and wraps them up in a standard framework so you can concentrate on the business logic of your app instead of the distributed architecture. You could build your own large, distributed, high reliability applications using Erlang’s concurrency primitives or even in another language, but eventually you’d find that you probably reinvented (poorly) OTP’s supervision trees, behaviors, applications, and releases.

The bit packing and unpacking syntax is also a hidden gem. I don’t have much cause to use it since I’m not processing binary file formats or speaking wire protocols very often, but I’ve done enough assembly and messing with bitwise operators in C in the past that I can appreciate how much it must simplify things.

The biggest problem I’ve had so far is just that google doesn’t seem to do a very good job finding erlang docs. Eg, searching for “erlang http” or “erlang http client” currently doesn’t turn up anything on the library in the Erlang standard library: [link] The Pragmatic book being out helps though.

Posted by Anders Pearson at

Sam Ruby: Erlang: First Impressions

[link]...

Excerpt from del.icio.us/tag/erlang at

Sam Ruby: Erlang: First Impressions

[link]...

Excerpt from del.icio.us/tag/erlang at

Sam Ruby: Erlang: First Impressions

[link]...

Excerpt from del.icio.us/tag/erlang at

Why do you post quite an old link to Mnesia (it seems to be a version included in R7B which is 7 years old)?
The latest should be [link] which is [link] if you use version numbers.

Posted by anonymous at

Why do you post quite an old link to Mnesia

Fixed.  Thanks!

Posted by Sam Ruby at

[link] [more]...

Excerpt from reddit.com: programming - newest submissions at

I’m really enjoying it so far.  The binary pattern matching is of great utility for url matching in places where you don’t need full regexp, too.

Posted by dbt at

Sam Ruby: Erlang: First Impressions

It seems to me that I only find a perfect summary for a language only when I’m done reading the entire manual. Happened with Ruby, and now again with Erlang. Thanks Sam. I appreciate it....

Excerpt from del.icio.us/rami/comments at

How odd - I wrote a post with the same title on the same day, although with less focus on the syntax and more on general ramblings. Anyway, I came here to read a different post and spotted this one by accident while trying in vain to locate an RSS feed - lots of interesting stuff here. Am I missing an obvious link, is it a secret URL, or do you just not do RSS?

Posted by Ciaran at

Am I missing an obvious link, is it a secret URL, or do you just not do RSS?

You appear to be running Firefox 2.0.0.6 on Windows XP.  Look for feed icon to right of the URL of this page in the navigation toolbar at the top of the page.  Click on it and you will see the feed.

I offer feeds for each post (which give you access to the comments for that page only), a feed to my comments page (which gives you all comments to any post), and a feed for my main page (which gives you only my posts).

Posted by Sam Ruby at

...to right of the URL of this page in the navigation toolbar...

Thanks for the tip - what an idiot I am. I’m so used to seeing a direct link on the page that I didn’t even notice it was there.

Posted by Ciaran at

Hehe, I didn’t know you were involved with Atom until just now, which makes my question above doubly idiotic doesn’t it. Sorry. :)

Posted by Ciaran at

Erlang impressions

Erlang: First Impressions — recommended reading if you’re thinking of learning Erlang, like me....

Excerpt from simulacra at

Reading list tab collections 1. erlang and haskell

Everyone is getting the erlang love thing.... Sam Ruby: [link] [link] And Haskell. Yeah its not the same thing... Three part OReilly series......

Excerpt from A steaming pile of... at

Erlang/OTP, Mnesia

1. Download [Erlang][erlang] 2. Setup [TextMate][textmate] with a [bundle for Erlang][erlang-bundle] 3. Read the [white paper][whitepaper] and the [book][programming-erlang] 4. Understand what [the]([link])...

Excerpt from Jeff Mesnil's Weblog at

links for 2007-08-28

InfoQ: Interview: Dan Pritchett on Architecture at eBay Martin Flowler and Floyd Marinescu intervierws Dan Pritchett about the impact of eBay’s scale on architecture, business relationships, organizational governance and software design. (tags:...

Excerpt from SDLC Blog at

Erlang and the future of programming

I recently ran across a post by Brendan Eich (he of Javascript fame) entitled Threads Suck, in which he talks about support for concurrency in future versions of Javascript. Speaking as someone who has a few years’ experience with serious...

Excerpt from Dubroy.com/blog at

Sam Ruby on Erlang

Sam Ruby a posté un long tutorial sur erlang . Dans les commentaires, il ajoute : “A platform designed from the ground up to support a “distributed DataBase Management System, appropriate for telecommunications applications and other … applications...

Excerpt from Ziki - Eric Cestari's last published content at

Should a site be built to scale when you’ve no idea if it will be successful?

Nati Shalom writes about the crisis of scaling that hits startups once they become successful. Most startups are unsure if their idea will be successful, so they do not, initially, waste any energy planning to scale up. Instead, they do what is...

Excerpt from Closer To The Ideal at

Why are you using *. instead of * in the example above?  (i.e. why not area({circle, R}) -> PI * R * R;)

Posted by djwonk at

Links for 2008-06-24 [del.icio.us]

matthew carriere: Using select, reject, collect, inject and detect. Ruby on Rails: scaling to 1 billion page views per month | Enterprise Alley | ZDNet.com Sam Ruby: Erlang: First Impressions Good comparison between Ruby and Erlang...

Excerpt from PaulBarry.com at

Add your comment