Do you like stories? Tales that move you out of the ordinary into the extraordinary. Do you take satisfaction in programming? Where every bit, every keystroke means exactly one very thing. Do you esteem transparent functionality above all else? Then read on, to get introduced to a land of farmers, magick and wizards. But its wizards are programmers and its sorcery is called F#…​

'Noh Seneco, wake up!' In the rays of the dawning sun, his mother stood next to his bed. In her hand she held a particular torch. The light was small, and grew smaller and smaller. 'We need new sourceries son, and quickly. Without it, we cannot do our work properly at the farm'. His eyes still full of sleep, Noh grunted: 'Can Tisrés not go? She like him a lot more than I do anyway…​' Yet his mother left him no choice: 'You are the eldest son, you will not use your sister to do your duty!'

So he went, with grumpy face, empty stomach and a bag of money. Through the tiny lanes, the silver streams and the lush meadows. The sun shown gently upon his face, the sky was clear deep blue. Soon he forgot all his spite, it was a perfect day after all. His mom was right of course, without sourceries, their farm would do no good. From the small torches that light without a flame to the plow that worked without a horse. Everything runs on magick juice. From the bottom of his heart, Noh has always been very proud of their farm. As far as he could remember, his da had used magicks to make the work easier. So different from all the other farms around them. They were still using donkeys and horses…​ Withal the astonishing things going on around their ranch, these magick sourceries were still a depletable source.

And that’s why he went, to the only provider who was into that kind of stuff in their neighbourhood. An old man, gray long hair, a dashing beard and weird hat. Even his name, Tion Fiwdal Zarunc, talked of grandeur and strange lands. Many people in town tried to avoid him as much as possible. Except for the Seneco family of course, as they needed him for his supply of magicks. Although it was surely not the first time Noh made this trip on his own, still the old man gave him shivers. When he was with his da, he did not feel this way. Why his sister Tisrés liked the man was beyond him. His tower was close now. The man did not live in a normal house by all means, no he had to be different.

wizard tower

The tower of wizard Tion Fiwdal Zarunc

Before he even knew it, the tower came into view. He waited a moment, then knocked twice. The door opened quickly and the aged wizard stood in front of him. 'I have been expecting you, eldest son of eldest son!' The voice of the magician was slow and deep. Noh’s uneasiness grow by the minute. 'Good day to you too, grand master Fiwdal Zarunc!', Noh replied hastily, 'I have come to buy some of your magick sources'. 'I know why you think you’re here. As I’ve already seen you move from afar, a new cart with sourceries is already send to your home. However, there is much more at play here boy! You have been chosen by the Secret Council of the Functional Ancients as my successor and protector of the realm.' 'I have been what?', Noh cried out. His heart started to pound like crazy. 'Successor? Protector of the realm? You mean I will become a bloody wizard, just like you? I am a just a meaningless farm boy in a small town!' Tion Fiwdal Zarunc, in all his might, looked him deep in the eye and said with a voice like rumbling of thunder within the mountains: 'Eldest son of eldest son, check. Farm boy from some insignificant town, check. Bearer of a heroes name, check. Unwilling, check. You hit all the marks, reluctant protégé! Furthermore, the Council has spoken, so there is no denying. You will do as commanded! Now, come on in.'

Noh had never been inside, the business was always done at the door. The room was enormous, even bigger than expected from the outside. Nearly all the walls were covered with shelves full of books, potions and pottery. A lifelike great orange cat with black stripes stood at the right, before a grand wooden table. Upon the table, between two burning candles, lay a thick old book opened at one of its last pages. Chest were basically seen everywhere. Before one wall, a gigantic device seems to sprung from the ground. A clawlike lever reached until the ceiling. A huge slightly bent black glass was positioned behind it. And on his left, he saw a transparent orb upon a pedestal.

wizard computer

The magickal apparatus

The stone seems to call Noh. It was like he had no power over his limbs anymore. He had to walk and look. 'I have never looked upon a wizard stone before', he said with trembling voice. The wizard, though cautious, did not stop him. He raised his voice and said: 'Lesson one. There is no such thing as magick. Magick is just a term to appease the common folk. What you will see is neither past nor future. It only gives you power to see the realm from a different perspective.' Noh stepped closer and started to look into the stone. First he saw his village from above, just like an eagle. The houses looked like little children’s toys. He saw his mother doing the laundry. Then the view swept away far into to mountains. Beautiful green slopes with white peaks. And at last, he saw shades and shadows of marching figures. With a cry he leapt back: 'I saw an army, full of nasty creatures and cruel beasts.' The magus sighed audibly and looked for a moment very, very old. But then he straightened his back, and began to talk. 'We magi feared this event for a very long time. You just added the last missing piece of the puzzle. Although the advancing army is still very far away, the time is nigh. There is work to do. Do you see that large device over there? It gives you power to produce an army. I will teach you the ropes, and you will be able to continue the work, even if I am gone.' He walked behind the apparatus, started to type and pulled the bizarre lever:

type Human = {Name:string}
let you = {Name="Noh Seneco"}

'It al starts by defining the thing, called a type. Once you know what you want to create, you can make of it as much as you want'. Noh looked at the screen. No longer was it black and empty, but filled with letters of all kinds of colors. It did not look that hard at all. 'So you created humans, and then another instance of me?' The wizard smiled for the first time since Noh stepped inside his tower, 'Smart kid, you are quite right. As we will need an army eventually, let’s also define strength and use it to label every human, so we know how strong our soldiers will be. What about this?'

type Strength = WEAK | NORMAL | STRONG
type Human = {Name:string; Strength: Strength }
let me = {Name="Tion Fiwdal Zarunc"; Strength=STRONG}
let you = {Name="Noh Seneco"; Strength=WEAK}

Noh looked at in wonder. 'This is amazing', he blurted out. Then he pondered: 'How can we give our soldiers multiple things? Like a shield and a sword?'. 'Easy', the sage answered. We just add list to define that. For the instances we can use square brackets divided with a semicolon. Together, you and I, we shall be the first army. I’ll carry my staff, you will wield your requested sword and shield':

type Gear = SPEAR | SWORD | BOW | HELMET | ARMOUR | SHIELD | STAFF
type Soldier = { Who:Human; Gear: Gear list }

let meAsSoldier = {Who=me; Gear=[STAFF]}
let youAsSoldier = {Who=you; Gear=[SWORD;SHIELD]}

type Army = Soldier list

let together = [meAsSoldier;youAsSoldier]

Suddenly Noh got a strange feeling inside. Something felt off. He looked at the wizard, and said: 'Once the enemy’s army is here, we cannot battle this army together! We could make more instances of ourselves beforehand, but still our army would be relatively small. So I was thinking, we can define types and instances with this apparatus, and then they exist. What about calling in extra resources?' 'Ah my boy, you do ask the right questions! See, I told you you were the right man for the job. It’s actually quite simple. Just as types can be defined, things that have not yet come to pass can be defined as well. We call those functions. Let’s start by defining a default spearman and swordsman creator. Once the time is dire, we can call onto them:'

let createSpearman () = {Who={Name="First Spear"; Strength=STRONG}; Gear=[SPEAR]}

let createSwordsman () =
  let human = {Name="First Sword"; Strength=WEAK}
  let gear = [SWORD;SHIELD]
  {Who=human; Gear=gear}

'There are a few things to notice here. We add the () to tell the apparatus it’s a function. This function does not have any dependencies, it can just be called upon once we need it. The calculations can be on either one or multiple lines. The only thing you need to remember, the last line returns the created object spontaneously. One thing I forgot to mention earlier, the apparatus is fairly intelligent. It can guess all given types automatically. Nevertheless, we can add those types ourselves, if need be':

let createSwordsman () : Soldier =
  let human : Human = {Name="First Sword"; Strength=WEAK}
  let gear : Gear list = [SWORD;SHIELD]
  {Who=human; Gear=gear}

Noh smiled when he saw the screen filled with all those marvelous functions. Knowing his time to learn was limited, he needed to know more. Much more. 'Dear wise master Fiwdal Zarunc, enlighten me. Not every soldiers can have the same name. Not every soldier should carry the same armour. How can we create an army where all those things are handed out at random?' The old man look up from the screen. His face turned stern and serious. He kept silence for a moment, until he finally opened his mouth: 'Learning too much too quickly will have repercussion, my apprentice. Too much information, and there is a grave chance you will remember even less. For the sake of time constraints, I will answer your question. What we can do is create a function that picks at random a number between two given numbers. It looks like':

let between x y = (new System.Random()).Next(x,y)
// or
let between (x: int) (y: int) : int = (new System.Random()).Next(x,y)

'That’s still easy to follow, the only real new thing is the dependencies of this function. That is x and y. Now it gets more complicated, so do not be scared. To pick let say a random gear from a list of armour, we need to define a pick-one-item-from-a-list function. Such implementation can look like this':

let pickOneRandom (list: 'T list) : 'T =
  let x = between 0 list.Length
  list.[x..x].Head

The boy looked at screen, filled with stuff he did no longer understand. 'Magick', he cried out loud. 'No, I told you before there is not such thing as magick. The first thing you don’t understand is probably the 'T type. It means it is a generic type, not yet known nor relevant for this function. It could be a Gear, a Human, a Soldier or any other type we defined at a certain point. And that’s exactly what we need, for we want to pick one random piece of gear from a list of gear. Now, the implementation part. The first line is quite easy, because it picks just a random number between zero and the length of the list. Then the second line. The first thing you need to know is that you can declare a range with the .. operator. We can use this operator to take a slice of the list. As x is just one random number, it actually creates a new list of one item. To return the item instead of the list itself, we take the Head, that is the first item, of the list. So no magick, only ignorance!'

Noh looked fascinated. Now he knew how to create random names and even random humans. All of a sudden he leapt to the console, started to type by himself and pulled the lever. The lever was heavier than expected. Once it was up again the screen showed:

let getRandomName () =
  let firstNames = ["Tom";"Liam";"Noah";"Oliver";"Elijah";"William";"James";"Benjamin";"Lucas";"Henry";"Alexander";"Olivia";"Emma";"Charlotte";"Amelia";"Ava";"Sophia";"Isabella";"Mia";"Evelyn";"Harper";"Luna";"Camila";"Gianna";"Elizabeth"]
  let lastNames = ["Atwater";"Agassi";"Draper";"Dwyer";"Botkin";"Benoit";"Biddercombe";"Fairchild";"October";"Vespertine";"Quill";"Xenides";"Idlewind";"McGillicutty";"Sacredmore";"Shields"]
  pickOneRandom firstNames + " " + pickOneRandom lastNames

let getRandomHuman () = {Name=getRandomName (); Strength=pickOneRandom [WEAK;NORMAL;STRONG]}

Tion Fiwdal Zarunc was impressed by the skill the boy showed. Never before in his long life had he seen a human pick up the Laws of Code and Magick so quickly. There was only one building block missing, then Noh could complete create a random army. 'To fill a list dynamically, you could use following construct Noh:'

let listOfText = [ for _ in 1..10 do yield "something"]

The apprentice grinned. It was not so hard after all. Very soon he had written the last needed functions:

let getRandomGear () =
  [ for _ in 1..between 1 7 do yield pickOneRandom([SPEAR;SWORD;BOW;HELMET;ARMOUR;SHIELD;STAFF])]

let createSoldier () = {Who=getRandomHuman (); Gear=getRandomGear ()}

let createArmy (amountOfSoldiers: int): Army =
  [ for _ in 1..amountOfSoldiers do yield createSoldier ()]

He clapped his hands for joy. With this apparatus he had the power in his hands. At the time of the enemies' arrival, he just had to execute createArmy [number] to generate an army! Suddenly his stomach made a rumbling sound. All the talk of coming armies and the introduction lessons of this great machine had let him forget his missing breakfast. Now that his body made remember him, fatigue overwhelmed him.`

'Oh dear, you are white like a ghost', blurted the old wizard. 'Go home, your mother is without a doubt already preparing lunch.' 'But what of the army? Are we ready? I can feel there is so much more to learn. I don’t feel ready at all!', said Noh in a small voice. Tion Fiwdal Zarunc looked calmly at the boy and answered: 'Do not fear young master. The darkness is still away. There is still time to prepare. You have learned enough for a day. Besides, your da is waiting for you to plow the fields. Remember, the sourceries are already home!' Slowly Noh started to walk to the door. The wizard was right, he had to go. Just before the door post he turned around and asked: 'What about the preparation? When will I be ready?' 'Even the very wise cannot give you an answer to that question. You will study here, every day until the enemy is here. Pray the gods it will be enough. Be gone now. Eat, work and rest, for you will need your strength tomorrow.' So the boy turned home with assured heart. There was still time. Time to learn, to study. There was still time to discover the mysteries known by the Secret Council of the Functional Ancients. And maybe, just maybe he could join them as well…​

Click here to download all the code and here to try F# yourself.
shadow-left