I learn by doing much more than by reading or listening, so as I go through O’Reilly’s Erlang Programming, I take the exercises at the end seriously. Generally, when I learn a new programming language, while I read, I’ll make a program of my own choosing, perhaps something that will be useful to me or that I could sell. However, with Erlang, I don’t even know where to begin using it yet. I want to get really, really good at it before I even attempt to make anything with it (I think remembering the mistakes I made with my first Ruby on Rails project caused this shift in attitude.. and let’s not forget the triumphs of reading Mastering Regular Expressions in its entirety).
I will be posting my attempts at the exercises, in case others might find it useful or seasoned Erlang programmers might tell me what I may be doing wrong. I’ll be spacing out my answers to chapter 3 over the week to keep things interesting. Here are the first three exercises from Chapter 3:
[erlang]-module(chapter3).
-export([sum/1, sum/2, even/1, create/1, reverse_create/1]).
-import(erlang).
sum(Num) when Num > 1 ->
Num + sum(Num – 1);
sum(Num) when Num == 1 ->
1.
sum(N,M) when N < M ->
M + sum(N,M-1);
sum(N,M) when N == M ->
N;
sum(N,M) when N > M ->
erlang:throw({‘EXIT’,{bad_argument}}).
reverse_create(N) when N > 1 ->
[N|reverse_create(N-1)];
reverse_create(N) when N == 1 ->
[1].
create(N) when N > 1 ->
create(N-1) ++ [N];
create(N) when N == 1 ->
[1].
even(N) when (N > 0) and (N rem 2 == 0) ->
io:format("Number:~p~n",[N]), even(N-1);
even(N) when (N > 0) and (N rem 2 == 1) ->
even(N-1);
even(N) when N == 0 ->
done.
[/erlang]
An Erlang Syntax Highlighter for your site: http://jldupont.blogspot.com/2009/06/erlang-syntax-highlighter.html
Thanks for the pointer!…updated
I had to modify it slightly to work w/ the version of syntaxhighlighter used by the wordpress plugin for it (one must be newer than the other.. they differ only in the camelcasing of the functions).