[Haskell-cafe] Four Fours problem in Haskell

Joachim Breitner mail at joachim-breitner.de
Mon Dec 4 12:37:41 EST 2006


This might be interesting for some of you (I hope):


An interesting programming puzzle appeard in the Blogosphere: To
describe every integer from 1 to 100 as a calculation involving only and
exactly four fours, and standard operations. There are solutions in C#
(code) and in python (code), so I tried to do mine in Haskell.

This seems to be meant for Haskell. The Type system makes the program
really great to read. Functional programming allows me to conveniently
create all possible associations of four operands with three binary
operations. (I found five. Why are the other two sources using four?)
And, in this program particular, the use of the list monad to quickly
generate a list of all possible combination is really a

The program is not too slow either:

$ time ./fourfour>/dev/null
real    0m0.917s
user    0m0.852s
sys     0m0.040s

And it could probably be sped up by using some kind of hashing or
similar stuffs. Note that the program has only 73 mostly quite short
lines, while the python code has 119 and the C# code has 138 of
relatively long lines (comments and empty lines not counted). I think
this is impressive (unless you have worked with haskell a bit, then you
are used to these kind of results). Comments welcome!


Joachim "nomeata" Breitner
  mail: mail at joachim-breitner.de | ICQ# 74513189 | GPG-Key: 4743206C
  JID: joachimbreitner at amessage.de | http://www.joachim-breitner.de/
  Debian Developer: nomeata at debian.org

More information about the Haskell-Cafe mailing list