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

Erlang version of "The Little Schemer"

I am learning Erlang, because I want to build web applications that scale easily. Before I can capitalize on Erlang's awesome concurrency support, I need to get comfortable with simpler stuff, like procedural programming.

Erlang at its core is a functional language. As I've noted previously, this means doing things quite differently than in C, Java, or PHP. Recursion is the key building block in functional languages. Thinking in terms of recursive functions is essential for getting anywhere in these languages.

I learned the basics of recursion in college, but it's not a tool I'd instinctively reach for. My attitude was, "Why bother?" Using recursion in most languages just makes the code harder to read, and purely-functional languages like Haskell are for writing PhD dissertations, not getting real work done.

Or so it seemed. Now I'm learning Erlang, and I want to get real work done.

"The Little Schemer", by Friedman and Felleisen has a reputation as a short, sweet book that teaches you to think recursively. That's quite an accomplishment for an approachable book. First published in 1974 as "The Little LISPer", it now uses Scheme, a modern variant of LISP.

I've seen this book translated into other languages before, notably Douglas Crockford's The Little JavaScripter. Since Erlang is a natively functional language, the rewrite should be even easier. Nobody else appears to have published such a thing.

So, here's my take. Call it "The Little Erlanger", if you will. :-)

Google Code: little.erl

I'm including unit tests, to prove that the code works. I also try to use "Good Erlang Style", as best I understand it. Sometimes Erlang offers a shortcut to the recursion-based approach, and I point to these in comments as "Erlang Preferred".

Currently complete through Chapter 7, will finish the rest ASAP.

PS: Erlang fans, I'm sure there is much room for improvement in my style. Any and all comments are welcome!