P16: a blog by Matt Kangas home archive
11 Jan 2008

Learning Erlang – How to iterate

One of my goals this year is to learn Erlang, a surprisingly old-but-new programming language that seems rather well-suited to "cloud computing". CouchDB is being written in Erlang, as is Amazon SimpleDB behind the scenes.

But... if you thought Ruby and Python code looks strange, look out! Erlang is a purely functional language. Unless you're comfortable with Scheme/Lisp, it will look very alien at first. No wonder some high-profile hackers are giving it a weekend spin and ending in frustration. I finished Chapter 3 in Joe Armstrong's book, the last he talks about sequential programming, and thought I'd try to write something straightforward – like a fractal generator. Fat chance: I got stuck counting from 1 to 10!

After sleeping on it, I realized I knew about recursion, but I hadn't imagined life when that was the only looping construct. And I wished I hadn't skipped that Scheme class in college! (I was EE, not CS.)

After another night's sleep, I decided to start by writing simple loops, printing 1 to 10, in as many ways as possible in Erlang. So far I've found seven variations, ranging from simple to unusual (Y combinator?). Here's the result so far. I'll be updating this as I learn more...

demo_counting.erl

PS: Someone needs to write a book called "Getting Things Done In Erlang", mimicing the structure of the Camel or Pickaxe book. Theory-first approaches won't cut it for the working-programmer crowd.