the barber of springfield

Have you heard of the famous Barber Paradox?

In case you have not – here is it retold with two of the best things everthe Simpsons and F#:

TL;DR here is a gist with the story.

We all know our favorite TV family the Simpsons:

type Simpson = int

let [<Literal>] Homer : Simpson = 0
let [<Literal>] Bart  : Simpson = 1
let [<Literal>] Lisa  : Simpson = 2

As Homer had a lot of jobs already it should not surprise you that he now works as a barber. But he has a strict rule: He will only shave those that don’t do it themselves.

The kids are surely old enough to shave (yeah even Lisa is in question … you remember her aunts?). So let’s help ourselves with some educated guesses and F# to figure it out:

let rec shaves b a =
    match a with
    | Bart  -> b = Bart
    | Lisa  -> false
    | Homer -> not (b |> shaves b)
    | _     -> false

let isShavedByHomer a =
    Homer |> shaves a 

Ok – so let’s check who’s among Homers clients:

> isShavedByHomer Bart;;
val it : bool = false

> isShavedByHomer Lisa;;
val it : bool = true

Ok – Bart is out … and Lisa – well I told you about Selma.

So what about Homer himself (we’ve seen him in 3D space and he gave a counter-argument to fermat’s last theorem so the question should not surprise you:

> isShavedByHomer Homer;;

Process is terminated due to StackOverflowException.

well DOH indeed …

And that ladies and gentlemen is the famous paradox expressed in F# and demonstrated by Homer …