Tag Archives: don syme

Einstein meets F# part 1

Recently, I began an attempt to learn the new .NET language, F#. I had never worked with a functional programming language before, and the C# 3.0 “functional” features had piqued my interest. I began by buying the excellent Expert F# 2.0 written for the F# 2.0 spec.  It is written by Don Syme the designer and architect of F#.  Its an excellent book, which I highly recommended.

I only have two small criticisms of the book which can probably be overlooked. First, it is a little unforgiving in making sure the reader is “up to speed” when combining concepts introduced in previous chapters.  This is a book where you will want to take notes, and make sure you understand the concepts before moving on.  Secondly, it has many chapters towards the end describing the basics of the .NET framework, which may be useful to some but are also a repeat of what I see in dozens of other books in the same family, e.g. .NET, Silverlight, C#, asp.net mvc, webforms, etc.

While the book was excellent, I do my best learning by doing and not reading.  So what program should be my first in F#?

I found a blog post in my daily barrage of code forum emails that looked interesting enough to try and solve with F#.  It described an approach to programmatically solving a riddle created by Albert Einstein. The riddle is as follows:

The Riddle

  1. In a town, there are five houses, each painted with a different color.
  2. In every house leaves a person of different nationality.
  3. Each homeowner drink a different beverage, smokes a different brand of cigar, and owns a different type of pet.

The Question

Who owns the fishes?


  1. The Brit lives in a red house.
  2. The Swede keeps dogs as pets.
  3. The Dane drinks tea.
  4. The Green house is next to, and on the left of the White house.
  5. The owner of the Green house drinks coffee.
  6. The person who smokes Pall Mall rears birds.
  7. The owner of the Yellow house smokes Dunhill.
  8. The man living in the center house drinks milk.
  9. The Norwegian lives in the first house.
  10. The man who smokes Blends lives next to the one who keeps cats.
  11. The man who keeps horses lives next to the man who smokes Dunhill.
  12. The man who smokes Blue Master drinks beer.
  13. The German smokes Prince.
  14. The Norwegian lives next to the blue house.
  15. The man who smokes Blends has a neighbor who drinks water.
First, I needed to decide how I could algorithmically solve the problem.  I decided upon a naive approach, of creating a list of every combination of the given home properties (i.e., color, pet, smoke, etc.). Than applying the given rules to obtain the list of valid houses, e.g. those that passed the given rules for a single house.  Than I would create sets of houses from all the permutations of remaining homes, and filter those based on the rules that apply to more than a single house.
In my next post, we will begin defining our domain objects.

To be continued in part 2.

Download the source code for this post.