Just a quick note to say that slides for my F# in Social Gam­ing talk at the recent CodeMesh con­fer­ence is now avail­able on SlideShare and that record­ing of the talk will be avail­able on InfoQ at a later date.

Share

Hello, just a quick word to say that slides for my F# in the Cloud and at Scale talk at the recent F# in Finance event is now avail­able on SlideShare and that I’ll be talk­ing about our use of F# in build­ing social games at CodeMesh next week.

CodeMesh is gear­ing up to be a great con­fer­ence, with some truly great speak­ers (with Dave Thomas, Joe Arm­strong, Pieter Hin­t­jens just to name a few!!) cov­er­ing a wide vari­ety of top­ics, hope to see you there!

Share

In F#, you can’t over­load a let–bound func­tion, and whilst it’s a hin­drance some­times you can gen­er­ally work around it eas­ily enough since you can still over­load mem­bers of a class. All you need to do is to wrap the mod­ule and con­stituent func­tions into a class and over­load the class mem­bers instead.

Multi-Arity Func­tions

This is where multi-arity func­tions come in in Clo­jure (an arity is sim­ply the num­ber of argu­ments that a func­tion can take).

One way to define multi-arity func­tions is to use the defn macro, for instance:

As you’ve prob­a­bly noticed, it’s pos­si­ble for one over­load to call another, so you can use this tech­nique to set default argu­ment val­ues as shown above. On its own, that’s not very inter­est­ing, and an effect that can be eas­ily repli­cated in F# using a com­bi­na­tion of optional para­me­ters and the built-in default­Arg func­tion with class mem­bers, even with­out resort­ing to over­load­ing (again, this is sadly only sup­ported with class mem­bers and not functions..).

Vari­adic Functions

What is def­i­nitely more inter­est­ing is Clo­jure’s sup­port for vari­adic func­tions – func­tions with infi­nite arity.

A vari­adic func­tion has a & sym­bol in its argu­ments list, which tells the com­piler that the next para­me­ter col­lects all remain­ing argu­ments as a sequence. So, if we rewrite the greet func­tion to take one or more argu­ments we can end up with a vari­adic func­tion such as:

As you can see, the argu­ments “Dar­ryl”, “Deepu” and “Tom” has been packed into the argu­ment rest in the func­tion def­i­n­i­tion and printed as a list (Clo­jure’s lists are enclosed in paren­the­ses ( ) just like its func­tion invo­ca­tions). If you come from a .Net back­ground then this behav­iour should be famil­iar to you, because that is how para­me­ter arrays works in .Net.

So what’s so spe­cial about vari­adic func­tions in Clo­jure then?

To quote Sebas­t­ian Shaw from the excel­lent X-Men First Class film:

image

In Clo­jure, there’s a built-in apply func­tion which allows you to ‘unpack’ a sequence and pass the unpacked val­ues to a func­tion as indi­vid­ual argu­ments. With this, we can now rewrite the vari­adic ver­sion of greet as fol­lows:

Notice that the argu­ments “Dar­ryl”, “Deepu” and “Tom” are no longer treated as a list but as indi­vid­ual argu­ments to the print func­tion, pretty neat. This SO answer also sheds some more light on why vari­adic func­tions exist in Clo­jure with some inter­est­ing exam­ples and idiomatic use of this type of functions.

 

Links

MSDN – F# Para­me­ters and Arguments

Clo­jure – Func­tional Programming

Clo­jure Docs – apply

SO – Why are many Clo­jure func­tions variadic

Clo­jure Cheatsheet

Learn X in Y min­utes – where X = Clojure

Share

I was recently invited to chat to the Mostly Erlang pod­cast about F#, with co-host Bryan Hunter, Fred Hebert (author of the excel­lent Learn You Some Erlang for Great Good!) and fel­low F# addict Phil Trelford (author of many pop­u­lar F# libraries such as Tick­Spec, Mini Rx and Foq).

It’s always nice to talk about sub­jects around F# and Erlang, espe­cially with crazy smart peo­ple like Bryan, Fred and Phil!

The show is now avail­able on the Mostly Erlang site here, hope you enjoy it!

Share

Prob­lem

The primes 3, 7, 109, and 673, are quite remark­able. By tak­ing any two primes and con­cate­nat­ing them in any order the result will always be prime. For exam­ple, tak­ing 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, rep­re­sents the low­est sum for a set of four primes with this property.

Find the low­est sum for a set of five primes for which any two primes con­cate­nate to pro­duce another prime.

Solu­tion

Note: the source code for both solu­tions are avail­able on github here.

 

Brute Force

This solu­tion runs in just over 29 sec­onds on my machine, not great, but within the 1 minute rule for Euler solutions.

 

Using Set intersections

Here’s an alter­na­tive solu­tion, using set intersections.

This solu­tion is slightly more effi­cient, run­ning in just over 17 sec­onds on my machine.

Share