The MIT Open Source license with a twist, Copyleft (ↄ)
As of today and after my biannual 1:1 meeting with our CEO and Head of
Department, I can now announce that our ƒ(x)RM (Functional Relationship
Management) will be released under the following Open Source License:
This is really good news, stay tuned for more information on ƒ(x)RM :-)
My background before using F# was OCaml, where you define your
interfaces/contracts (signatures in F#) in a .mli file (.fsi files in
F#) as shown in the code below:
Afterwards the following function must be implemented in the .ml file sharing
the same name as the .mli file. The main issue here is that you needed to
separate the definition of your functions to several files as the project
grow. What I like about Scott’s approach, is that you can still maintain the
entire domain in the same file while you still are able to define you explicit
fields in your signature files:
Once I understood why it was smart to take this approach, I then decided that I
wanted to implement my functions as types. As I looked through Scott’s amazing
F# for fun and profit, I really didn’t find
any example on how to implement this. I therefore sent and e-mail to Scott and he
was really humble and helpful and provided me this piece of code:
So the trick was just to implement your function types as lambdas, which is
With this receipt in mind I decided that I wanted to implement a card-game based
on the card domain provided by Scott, I ended up modifying it a bit. The chosen
game was: War, probably the easiest game to play and (maybe) therefore also
the easiest game to implement,
I searched for war card game on Google and the following website showed up:
The rules are described as: In the basic game there are two players and you use
a standard 52 card pack. Cards rank as usual from high to low: A K Q J T 9 8 7 6
5 4 3 2. Suits are ignored in this game.
Deal out all the cards, so that each player has 26. Players do not look at
their cards, but keep them in a packet face down. The object of the game is to
win all the cards.
Both players now turn their top card face up and put them on the table. Whoever
turned the higher card takes both cards and adds them (face down) to the bottom
of their packet. Then both players turn up their next card and so on.
If the turned up cards are equal there is a war. The tied cards stay on the
table and both players play the next card of their pile face down and then
another card face-up. Whoever has the higher of the new face-up cards wins the
war and adds all six cards face-down to the bottom of their packet. If the new
face-up cards are equal as well, the war continues: each player puts another
card face-down and one face-up. The war goes on like this as long as the face-up
cards continue to be equal. As soon as they are different the player of the
higher card wins all the cards in the war.
The game continues until one player has all the cards and wins. This can take a
As I mention above, I made a few additions and subtractions to the domain. As
Scott’s also says in the video, that this approach is so clear and concise that
should be enough to understand what the application will do, so I’m not going to
explain the model.
The generic utilities that are not only related to this domain
In order to make the implementation easier and more clean I can already
think of a couple of functions that could be useful to a card game
implementation: random and shuffle for mixing the cards, Cartesian product
to create the deck of cards, etc.
And finally to the implementation of the game. As we use to state in the F#
Community, code should be easily readable. Please don’t hesitate to leave
comments at the bottom of blog post if you don’t understand some parts of the
code and I’ll try to explain it as best as possible. Remark: I might have a
bad habit of using very small, usually a letter or two, to define my values,
please bare with me.
printfn"The winner is: %A"(CardGameWar.simulation4)
So based on this approach, the whole application can be encapsulated in types
which is really, really, really cool !!!
In order to watch who wins and collects each pile, I added a print statement in
the let pickPile : PickPile function. The output can be seen below:
The last part out-putted by the game is the winner, in this case “d”, who ends
up with all the 52 cards in his deck.
War by Edwin Starr (1969)
Finally but not least, we need to remember what war is really good for: “War,
huh, yeah What is it good for Absolutely nothing Uh-huh War, huh, yeah What is
it good for Absolutely nothing Say it again, y’all”.
MSDN: Lists in F# are
implemented as singly linked lists, which means that operations that access
only the head of the list are O(1), and element access is O(n).
Wikipedia: In Standard
ML, lists are
represented as imbalanced binary trees, and thus it is efficient to prepend
an element to a list, but inefficient to append an element to a list. The
extra pass over the list is a linear time operation, so while this technique
requires more wall clock time, the asymptotics are not any worse.