Posts

Showing posts from July, 2009

F#: Why the rec?

I've been looking into F# for a while now, and I think it's an awesome addition to the .NET framework. It's more expressive and concise than C#. It's functional by heart, with discriminated unions, pattern matching, tail-call optimization, and it also supports object oriented programming (being a .NET language, it just had to) and imperative programming. Its type inference creates the feel of a dynamic language, but in a strongly-typed language (much like in Haskell and Scala). That said, one of the first things that I noticed is that in order to declare a recursive function you have to be explicit about it in the function definition. You have to declare it with the rec keyword. So, this is a non-recursive function: let add a b = a + b The function name is add, and it takes 2 (inferred) integer parameters, a and b, and returns their sum. You call it like this: add 1 2 // returns 3 Now, this is a recursive function: let rec add_all l = match l