Just a quick note to say that slides for my F# in Social Gaming talk at the recent CodeMesh conference is now available on SlideShare and that recording of the talk will be available on InfoQ at a later date.
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 available on SlideShare and that I’ll be talking about our use of F# in building social games at CodeMesh next week.
CodeMesh is gearing up to be a great conference, with some truly great speakers (with Dave Thomas, Joe Armstrong, Pieter Hintjens just to name a few!!) covering a wide variety of topics, hope to see you there!
In F#, you can’t overload a let–bound function, and whilst it’s a hindrance sometimes you can generally work around it easily enough since you can still overload members of a class. All you need to do is to wrap the module and constituent functions into a class and overload the class members instead.
This is where multi-arity functions come in in Clojure (an arity is simply the number of arguments that a function can take).
One way to define multi-arity functions is to use the defn macro, for instance:
As you’ve probably noticed, it’s possible for one overload to call another, so you can use this technique to set default argument values as shown above. On its own, that’s not very interesting, and an effect that can be easily replicated in F# using a combination of optional parameters and the built-in defaultArg function with class members, even without resorting to overloading (again, this is sadly only supported with class members and not functions..).
What is definitely more interesting is Clojure’s support for variadic functions – functions with infinite arity.
A variadic function has a & symbol in its arguments list, which tells the compiler that the next parameter collects all remaining arguments as a sequence. So, if we rewrite the greet function to take one or more arguments we can end up with a variadic function such as:
As you can see, the arguments “Darryl”, “Deepu” and “Tom” has been packed into the argument rest in the function definition and printed as a list (Clojure’s lists are enclosed in parentheses ( ) just like its function invocations). If you come from a .Net background then this behaviour should be familiar to you, because that is how parameter arrays works in .Net.
So what’s so special about variadic functions in Clojure then?
To quote Sebastian Shaw from the excellent X-Men First Class film:
In Clojure, there’s a built-in apply function which allows you to ‘unpack’ a sequence and pass the unpacked values to a function as individual arguments. With this, we can now rewrite the variadic version of greet as follows:
Notice that the arguments “Darryl”, “Deepu” and “Tom” are no longer treated as a list but as individual arguments to the print function, pretty neat. This SO answer also sheds some more light on why variadic functions exist in Clojure with some interesting examples and idiomatic use of this type of functions.
I was recently invited to chat to the Mostly Erlang podcast about F#, with co-host Bryan Hunter, Fred Hebert (author of the excellent Learn You Some Erlang for Great Good!) and fellow F# addict Phil Trelford (author of many popular F# libraries such as TickSpec, Mini Rx and Foq).
It’s always nice to talk about subjects around F# and Erlang, especially with crazy smart people like Bryan, Fred and Phil!
The show is now available on the Mostly Erlang site here, hope you enjoy it!
The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property.
Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.
Note: the source code for both solutions are available on github here.
This solution runs in just over 29 seconds on my machine, not great, but within the 1 minute rule for Euler solutions.
Using Set intersections
Here’s an alternative solution, using set intersections.
This solution is slightly more efficient, running in just over 17 seconds on my machine.