January 2007 - Posts

First class functions and currying

The paradigm behind functional programming says that everything can be solved creating and combining functions. Mathematical functions that is, like in "a transformation that takes values from a domain and map them to a codomain". For example, the domain of the length function is the set of all possible character strings and its codomain are the positive integers, the sin function has as domain all the angles and as codomain the reals between -1 and 1. A couple of function definitions in F#:

let square n = n * n
let cube p = p * p * p

With this definitions square 5 gives 25 and cube 3 is 27. While at it, it's worth noting that the type of square is int -> int, which is the F# way of saying that the domain of square is all the integers and so is its codomain. Furthermore, in languages like F# or Haskell functions are first class citizens or, for short, they have first class functions. Among other things, this means that a function can be used in any place where any other value (be it an integer, a double, a string, etc.) can. For example, a function can be passed as argument or it can be returned from a call or it can be the result of an expression. To see where this fact can leads us to, let's consider this generalization of square and cube:

let rec raise power value = if power = 0 then 1 else value * raise (power - 1) value

The raise function defines value ^ power based on the facts that value ^ 0 = 1 and value ^ power = value * (value ^ (power - 1)). The type of raise is int -> int -> int, intuitively, this says that raise takes two integers and produces a third one. But the type of raise can also be interpreted like this: int -> (int -> int), that is, its domain is the integers and its codomain are the functions of type int -> int. Say what? Well, for example, raise 10 doesn't give any number but a function, and sure enough, if we ask F# to evaluate raise 10, it does it without a glitch, and instead of printing a number it explains that the result is a function of type int -> int. This interesting fact let us make these definitions:

let square = raise 2
let cube = raise 3

That is, the square function is equivalent to the raise function with its first argument fixed at 2. We just defined a new function from another function to which we did *not* give all its arguments! This operation of invoking a function with only a part of its arguments is called partial application or currying, after Haskell Curry. square and cube are silly examples of the power and elegance that we can attain with F#, with any luck I'll show you more practical examples in future posts.

Intriguing but terribly irrelevant? May be not so: the upcoming versions of C# and VB.NET will be able of currying all right. Something nice is cooking at Orcas.

Video resources for getting your feet wet with functional programming

Since, by pure coincidence, I learnt some LISP and functional programming. languages like Haskell have intrigued me. Now, this kind of academic interests happen to have practical ramifications that may be of interest to many of you. For example, C# 3.0 will have features like lambda functions and type inference which are abilities brought from functional languages, furthermore, Orcas LINQ, the new proposal for integrating generic queries into C# and VB.NET semantics, is a direct application of functional concepts. This video is a fascinating conversation on the relevance of such concepts for solving actual problems like using the potential of multi-core CPUs or handling concurrency in transparent ways, people like Anders Hejlesberg feature on the video, so it's worth downloading. After watching the video I think many of you will want to learn some more about functional languages and, from the many available options, may be the closest one is F#, which was created at Microsoft Research and runs inside Visual Studio, I suggest to start with this very gentle video which is an eye opener, later on you can watch Don Syme (the creator of F#) talking about the ideas behind the language here and then he shows us the language here (although for starters I prefer the Flying Frog Video). Happy lambda programming!

More Posts