My preferred approach to learning anything is by diving in. Give me problems to solve. This approach has, at times, brought complication and peril into my life. But not with programming languages. This is still the way I like to go. So now that I’ve finally come up with some time to tackle a bit of functional programming, I went and found me a couple of tutorials (Erlang and Haskell), and started to cast about for problems.
Fortunately, the Web is full of problems. I mean that last statement in so many different ways. Particularly, though, new programmers have many options for taking on new languages and algorithms through problem sets. TopCoder is a great choice (with a very cool competition applet, but a limited choice of languages), and there may be nothing better than the Online Judge from la Universidad de Valladolid.
Project Euler is extremely good, however, for first getting your feet wet with a new language because the mathematically oriented problems typically have well-defined constraints and checking the answer is trivial. Also, because you only submit answers and not code, there’s no language limitation. Use whatever your heart desires.
Project Euler’s first problem is a cakewalk, but it was a good start for my Haskell hacking. The problem — to add all the numbers below 1000 which are multiples of 3 or 5 — is solved with a one-liner in the Haskell interpreter, although the standalone program requires a few extra lines for setup.
-
module Main where
-
-
The Erlang version is almost identical.
-
-module(problem1).
-
-export([solve/0]).
-
-
solve() -> lists:sum([X
X <- lists:seq(1,999), -
(X rem 3 == 0) or (X rem 5 ==0)]).
Please keep in mind that I don’t claim anything like proficiency in either of these two languages, and it’s highly likely that there are better ways to solve these problems. But I was forced to go searching through tutorials and docs in order to figure out how to get the answer, and in the process I learned a lot.
The power and ease of list comprehensions in Haskell and Erlang blows me away. Of course, list comprehensions are available in Python as well. The Python solution is, once again, almost identical to the other two languages, suggesting that if there’s more than one way to skin a cat, well, there’s also a best way, too.
-
print sum([x for x in range(1,1000) if x%3 == 0 or x%5 == 0])
So head on over to Project Euler and knock some of those problems out of the park using the language you’ve been meaning to learn for some time now.

