F# Syntax You Didn’t Know (Maybe)

by kiteason26. February 2014 11:09

Everyone plateaus a bit when learning a new language.  It’s important to guard against that and keep learning!  To that end here are six F# tricks which - maybe - you haven’t come across yet.  Enjoy.

1) Strings are sequences

Because they implement IEnumable, strings are sequences.  Which means that you don’t have to ‘manually’ pull out individual elements and process them ‘char by agonizing char’.

val ToMorse : s:string -> string

> ToMorse "ABACAB";;

val it : string = ".- -... .- -.-. .- -... "

>

2) You don’t always need to say (fun x -> …)

If the arguments of the function you want to call in something like a Seq.map or Seq.iter operation are the same types and order as those required by the higher-order-function in question, you don’t need to map the arguments across with the (fun x -> …) syntax.

It’s probably clearer with some examples:

3) You can pattern match in for-loop arguments

This is a naughty one!  If you use the syntax

for <Discriminated Union Pattern> in <Sequence of Discriminated Unions> do

...then the body of the loop will iterate only for those sequence items which match the DU Pattern.  This is so naughty that you’ll get a compiler warning and Jon Harrop will tell you off, but still…

4) You can pattern match on many bizarre combinations of list items

5) You can do comprehensions of floating point numbers

This is another one that is not without its risks but… you can increment values in a list or array comprehension by things other than integers.

val xValues : float [] = [|0.0; 0.1; 0.2; 0.3...

6) Active patterns can use active patterns

val PrimaryColorWays : (RainbowColor * RainbowColor * RainbowColor) [] =

[|(Red, Orange, Yellow); (Violet, Green, Orange); (Violet, Orange, Red)|]

OK, so who can shoehorn every single one of these into one application?

Tags:

The Beautiful Camel

by kiteason19. February 2014 13:15

Recently, I’ve been thinking a lot about a couple of things.  The first is a tweet I saw expressing amazement that anyone should think F# syntax is ‘ugly’.  The second is the utterly wonderful documentary series ‘Wild China’ which has been airing here in the UK.  Under the influence of slightly too many Valentine’s day coffee-chocolates, the two steams of thought got slightly mixed up in my head.  Here is the result.

The Beautiful Camel

There was once a young merchant whose family travelled the Silk Road from China to the Mediterranean.  One year, the young merchant, being young and still somewhat foolish, tarried a little too long in the perfumed houses of Persia, and his caravan, not wanting to be caught in the mountains in winter, left without him.

The young merchant was not worried: he had with him a fine horse, Warda, that he had ridden since childhood.  Warda knew his every whim, and he the horse’s.  It would present little difficulty to catch up with the caravan.  So he set off confidently enough, and, pausing only to ask the occasional oncoming traveller how far ahead the caravan was, he and Warda ate up the eastward miles.

But a strange thing happened.  Although Warda’s hoofbeats echoed briskly across the landscape, each oncomer would give him the same news: “You are three days behind.”  Before long it was “You are four days behind”.  Eventually the young merchant found himself on the fringes of the Gobi Desert, no closer to his caravan than when he had set out a month before.

Presently he came to an oasis, where the Silk Road angled northward to skirt the desert.  Frustrated by his lack of progress, the young merchant decided to spend the day at the oasis, before heading directly across the Gobi.  By travelling at night, and keeping the north star (he knew it as al-Jadi) to his left, he could navigate well enough.  Thus he would cut a great corner off the Silk Road’s route, and be sure to catch up with the caravan.

As he rested among the palms, an old man approached him.  The young merchant kept his hand on his dagger, but the old man seemed friendly enough.  When the merchant confided his plight - and his plan for a short cut - the man nodded.  The young merchant was a little surprised, expecting to hear the usual blood-curdling stories of singing sand dunes and deadly mirages.  Instead, the old man seemed almost encouraging.

“There is just one thing, my friend”, said the old man, chewing a fig.  “Your horse, magnificent though she is, is not a suitable mount for this journey”.

The young merchant grew angry.  “I know how this goes”, he thought.  “He will offer me some miserable pack animal in exchange for my beloved Warda.”  But he held his tongue, and the old man went on.

“I see what you are thinking,” said the old man.  “That I seek to take your steed.  But no, you may keep the horse, though she is beautiful.  All I ask is that you take with you also one of my camels.  It has been a good breeding year and I have more than I know what to do with - you may take your pick of them as my gift.  Think of me as a camel contributor.”

But the foolish young merchant would have none of it.  The camels were ugly and unfamiliar, and he had no time to learn how to control one.  He dismissed the old man, and settled down to rest in the shade.  In the evening, as he was filling his water-skins, the old man returned.

“I beseech you: take a camel!  These shifting sands are no place for a horse.  Besides, the camels know the routes across the desert.  They will lead you.”  But the camels, even with the benefit of a brilliant moon, looked no better than they had in the day, and the merchant had heard - was it from a horse dealer? or a farrier? - that they would spit in your eye if they did not like you.  He dismissed the old man with a less than subtle gesture towards his dagger.

And so the young merchant set off across the dunes, keeping al-Jadi to his left.  In the first hour he made great progress.  He and Warda were as one, the horse anticipating perfectly his chosen route through the scattered rocks and shallow dunes.  A camel indeed, for a fine merchant such as he!  Such foolishness in that oasis!

But steadily, the landscape changed.  The dunes became steeper, higher; the rocky places scattered with stones that seemed exactly the right size to break Warda’s pace.  The moon was past its zenith and the dark shadows it cast were lengthening.  He urged Warda on, despite her increasingly stumbling gait.  Far too soon, the sun began to rise.  Al-Jadi faded from view and it would soon be too hot to travel.  He urged Warda into the shade of a large rock and prepared to sit out the baking day.

He was woken by Warda’s laboured breathing.  She seemed to having some sort of fit, foaming at the mouth and thrashing about.  He braved the scything limbs and tried to get her to drink from one of his water skins.  It was futile.  Warda gave out one last despairing gasp and lay still.  Infuriated, the merchant took his belt and started to beat the animal.  But it was in vain - his entire caravan could have returned and flogged the poor horse until the Spring and it would have done nothing to revive the poor creature.

Appalled at what the desert had done to him - and so quickly! - the merchant fell to his knees and wept.  How could it have come to this?  All his ancestors, for as long as time could remember, had been horseman; horses had served them well in every conceivable circumstance.  It wasn’t fair!

Presently the sound of sliding rocks and the snort of a large animal roused him from his self pity.  He looked up to see the silhouettes of two large camels, the leader ridden by the old man from the oasis, striding confidently down towards him.  For the first time he appreciated how perfectly suited the creatures were to this place.  The wide splayed feet stood on the sand rather than sinking into it.  The thick hair shrugged off the dazzling sunlight as it would shrug off the freezing night.  The old man, he saw, was not as old as he had seemed in the oasis.  He called a greeting to the merchant.  He seemed to have a slight Australian accent, which was odd, as Australia wouldn’t be rediscovered for another half-millenium.  The man pulled back his tagelmust and smiled sardonically.

“Do you still think my camels are ugly?”

Tags:

Widget TextBox not found.

Unable to cast object of type 'ASP.widgets_textbox_widget_ascx' to type 'App_Code.Controls.WidgetBase'.X

Month List

Page List

Widget Blogroll not found.

Unable to cast object of type 'ASP.widgets_blogroll_widget_ascx' to type 'App_Code.Controls.WidgetBase'.X