## Blog: Ramón Soto Mathiesen

### Code Snippet:

 1 module Helpers =
2   let permutations ls =
3     let rec insertions x = function
4       | []             -> [[x]]
5       | (y :: ys) as l -> (x::l)::(List.map (fun x -> y::x) (insertions x ys))
6     let rec permutations' = function
7       | []      -> seq [ [] ]
8       | x :: xs -> Seq.concat (Seq.map (insertions x) (permutations' xs))
9     ls |> permutations'
10
11 module PrisonersHatsPuzzle =
12   type Hat = Red | Blue
13   type Prisoner = Hat
14   type Prisoners = Prisoner * Prisoner * Prisoner * Prisoner
15   type Guess = Prisoners -> Prisoner * Hat
16
17   // Bullet-proof logic for the prisoners to always go free:
18   //
19   // As the fourth prisoner is behind a screen, we don't care about him.
20   // If the last prisoner sees two equal colored hats, he knows he has the
21   // the opposite and hereby he can call out the color.
22   // In case the last prisoner doesn't call out his color, the second from
23   // behind will know that he doesn't have the same color hat as the prisoner
24   // in front of him, else the last one would have called it, hereby, he
25   // can safely call out the opposite color of the prisoners hat in
26   // front of him.
27   //
28   let guess : Guess =
29     fun prisoners ->
30       let a,b,c,d = prisoners
31       match a,b,c,d with
32         | (_,Red,Red,_)   -> a,Blue
33         | (_,Blue,Blue,_) -> a,Red
34         | (_,_,Blue,_)    -> b,Red
35         | (_,_,Red,_)     -> b,Blue
36
37 open Helpers
38 open PrisonersHatsPuzzle
39
40 let hats = [Hat.Red; Hat.Blue; Hat.Red; Hat.Blue;]
41
42 let alwaysGoFree =
43   permutations hats
44   |> Seq.map(fun xs ->
45     match xs with
46       | a::b::c::d::[] -> a,b,c,d
47       | _ -> failwith "never")
48   |> Seq.map(fun x -> guess x)
49   |> Seq.toArray

### Code output:

module Helpers = begin
val permutations : ls:'a list -> seq<'a list>
end
module PrisonersHatsPuzzle = begin
type Hat =
| Red
| Blue
type Prisoner = Hat
type Prisoners = Prisoner * Prisoner * Prisoner * Prisoner
type Guess = Prisoners -> Prisoner * Hat
val guess : Prisoner * Prisoner * Prisoner * Prisoner -> Prisoner * Hat
end

val hats : Hat list = [Red; Blue; Red; Blue]

val alwaysGoFree : (Prisoner * Hat) [] =
[|(Blue, Blue); (Blue, Blue); (Blue, Blue); (Red, Red); (Red, Red);
(Red, Red); (Blue, Blue); (Red, Red); (Red, Red); (Red, Red); (Blue, Blue);
(Red, Red); (Red, Red); (Red, Red); (Blue, Blue); (Blue, Blue); (Red, Red);
(Red, Red); (Blue, Blue); (Blue, Blue); (Blue, Blue); (Blue, Blue);
(Blue, Blue); (Red, Red)|]

### First thing first

Before you can create your own F# logo, you will need a drawing/editing program with support for layers. If you work on a Windows box, you might as well go with Paint.NET - Free Software for Digital Photo Editing, but if you are on a *nix box as myself, you might want to give Pinta: Painting Made Simple a try.

Your logo file should preferable be in a looseless format and with the following measurements: 200 pixels x 200 pixels.

from the The F# Software Foundation Logo for F#. You should have the following in a local folder by now:

[ mon@mbai7 logos ] ll
total 20K
-rw-r--r-- 1 mon staff 7.7K Oct 11 21:02 delegate_frm_logo.ora
-rw-r--r-- 1 mon staff 4.0K Oct 11 20:37 delegate_frm_logo.png
-rw-r--r-- 1 mon staff 1.3K Oct  6 14:56 fsharp100mcdark.png
-rw-r--r-- 1 mon staff 1.5K Oct  6 14:56 fsharp100mclight.png
[ mon@mbai7 logos ]

### Let’s do it!

The initial logo looks like this:

Once the file is open in Pinta, we will create a new file with the same dimensions and save it with the OpenRaster format (.ora). This format supports layers, which is indispensable for this kind of work. We will create an empty layer called frm and we will import the fsharp100mclight.png as a layer (Menu |> Layers |> Import from File…)

We will now choose our logo file and select all and copy it.

Afterwards, we will paste it into the frm layer. As the frm layer is below the transparent F# logo layer, the F# logo will still be visible (slightly).

The reason the logo is not blending in well is because I have a black background. In order to make the logo visible, I will change the colors to dark green and light green.

And you are done:

Easy right? Btw I tried out changing a bit the colors in order to comply with the terms of use:

… I’m not convinced though.

Update: @ReedCopsey sent me the following logo which is pretty much awesome!!!

I have no clue how he has done it, but at least it doens’t violate the terms of use. The lesson learned here is that the F# Comunity will help you out if you aren’t able to do it yourself which is pretty cool.

### It wasn’t that bad, Nykredit only lost 650.000 USD but …

A couple of days ago, a Danish Finance Institution, Nykredit, was in the media due to a stock mistake: Stock mistake costs Nykredit millions. The mistake was that instead of selling 4 A.P. Møller - Mærsk B at 14.000 DKK/stock, the stock broker sold 14.000 shares to the price of 4 DKK/stock.

A bit of simple mathematics will show us how bad that deal was:

• 14000 x 14000 - 14000 x 4 = 195.944.000 DKK

in looses, more or less 33 million dollars. The finance institution was lucky that the deal was made in the last five minutes before the close of trading, where all deal are secret and the final price is set when trading in closed, which in this case was 13.980 DKK/Stock. Nykredit only lost 3.780.000 DKK, about 650.000 USD, which given the circumstances wasn’t that bad.

You might be thinking: “How the heck can a Finance Institution make such a big mistake, aren’t these guys supposed to be looking after our money?” but then you also might think: “Well these greedy bastards had a lot to do with the financial crisis so …” The main issue here is, why would the stock broker even be allowed to make this kind of mistakes? The answer would be that most business critical solutions are still made with an old fashioned approach.

I will, in very few lines of F# code, show how this kind of mistakes would never be possible by using their Units of Measure to construct a very simple but correct and robust DSL (Domain specific language):

 1 open System
2
3 [<Measure>] type DKK
4 [<Measure>] type STOCK
5
6 let priceMaerskB : int<DKK/STOCK> = 14000<DKK/STOCK>
7
8 let buyPrice  (stocks:int<STOCK>)                        = stocks*priceMaerskB
9 let sellPrice (stocks:int<STOCK>) (price:int<DKK/STOCK>) = stocks*price
10
12 sellPrice 4<STOCK> 13900<DKK/STOCK>

In the example above, the stock broker needs to explicitly assign to each of the used integers, a corresponding units of measure tag. The result is what we intended it to be, which we also don’t need to make any kind of unit test, as we have ensured that is not possible to represent an illegal statement with our domain:

[<Measure>]
type DKK
[<Measure>]
type STOCK
val priceMaerskB : int<DKK/STOCK> = 14000
val buyPrice : stocks:int<STOCK> -> int<DKK>
val sellPrice : stocks:int<STOCK> -> price:int<DKK/STOCK> -> int<DKK>
val it : int<DKK> = 56000
val it : int<DKK> = 55600

Sadly, there are still to many people, in charge, that live in the past and keep banging their heads against the same walls over and over again instead of taking a ladder and climb over them once and for all … Anyway, if you want to read a few testimonials on how using this kind of approach, please visit: