In my previous post, I talked about some of the basics of recursion and why you might want to use it to your advantage.  Today, let's dive a little deeper into the different kinds of recursion, including linear, tail recursion and finally binary recursion.  This is in a series of back to basics covering recursion in some depth.

Starting Off

Where we left off is to take a simple imperative statement and make it not only recursive, but we could also hypothetically turn it into tail recursive as well.  Let's start off with the simple, yet overused factorial example in a very imperative way using looping:

C#
public static int Factorial(int n)
{
    var fact = 1;
    var i = n;

    while (i > 0)
    {
        fact = fact * i;
        i--;
    }

    return fact;
}

F#
let factorial_imperative n =
  let mutable fact = 1
  let mutable i = n
 
  while i > 0 do
    fact <- fact * i
    i <- i - 1

  fact

So, what we're left with is mutation galore.  That's perfectly ok in the imperative world, but it doesn't make sense to me in the functional world since we have such things as recursion.  Functional languages make you go out of your way to make values mutable, because by default they aren't mutable at all.

Linear Recursion

Linear recursion is by far the most common form of recursion.  In this style of recursion, the function calls itself repeatedly until it hits the termination condition. After hitting the termination condition, it simply returns the result to the caller through a process called unwinding.  Most of my samples I posted in the previous post followed this way of recursion.

C#
public static int Factorial(int n)
{
    if (n <= 1)
        return 1;
    else
        return n * Factorial(n - 1);
}

F#
let rec factorial_linear n =
  if n <= 1 then 1 else n * factorial_linear (n - 1)

As you noticed, the last call here is a calculation, which helps unwind itself to the termination condition of 1.  Of course it's also guarded against negative input as well which would cause an infinite loop, which would be pretty bad.  But with large input, this could be a problem.

Tail Calls

As I've mentioned before, it's pretty important to think about the stack when you do recursion.  For the reasons of stack overflows, it's pretty important to mention.  When you call an F# function, stack space is allocated and then freed when the function returns, or, when a tail call is performed.  We have to be aware of this, because a very deep set of nested function calls will cause a StackOverFlowException to be thrown.  Below is a simple example on how this can occur. 

#light

let rec recursiveFunc i : unit =
  if i >= 1000000 then
    ()
  else
    if i % 1000 = 0 then
      printfn "Recursing at %i" i
    recursiveFunc (i + 1)
    printfn "Just called the function with %i" i

recursiveFunc 100

The above function actually recurses, but that's not the last thing the function does.  It in turn calls a printf end line function to display the result.  If you let this run, you'll get a nice StackOverFlowException and it will take down your F# Interactive (fsi) session.  How do you fix this situation?  Well, it's a topic called tail recursion which will expand upon this.

Tail Recursion

Tail recursion is a specialized form of the linear recursion where the last operation of the function happens to be a recursive call.  The difference here is that in my previous samples, I've been calling functions which perform a calculation on the result of the recursive call.  That could lead to stack overflows should the recursion get too deep.  Instead, I don't want to do any work during the unwinding phase, just return the value from the function.

Let's take the above sample and make it tail recursive:

#light

let rec recursiveFunc i : unit =
  if i >= 1000000 then
    ()
  else
    if i % 1000 = 0 then
      printfn "Recursing at %i" i
    recursiveFunc (i + 1)

recursiveFunc 100

All I had to do was get rid of the last statement and now the last statement is simply a calling of the function again with no work performed.  If I run it through the F# interactive again, I get no problems at all, and it runs through smoothly.  Let's refactor our factorial to be tail recursive in F#, and then look at a C# solution.

F#
let rec factorial_tail n =
  let rec factorial_inner acc x =
    if x <= 0I then acc
    else factorial_inner (x * acc) (x - 1I)
  factorial_inner 1I n   

As you can see, the last calls to my recursive inner function is indeed tail recursive as no work is being done to the result of any calls.  From there, my outer function should be able to call passing in the accumulator of 1 and the number we're passing in.  Since I'm using BigInt, I don't have a problem with overflow either in regards to really large input.  This is something that the .NET BCL was working on, but kept it internal in the System.Core.dll it seems.  But, F# was nice enough to give us BigNum and BigInt implementations, as F# tends to be rather math centric.

The Problem with C# and Tail Calls

Now turning our attention to the C# counterpart to this, let's try our implementation of the above functionality in it.  Instead of just another function, I'll inline the accumulator function inside as an anonymous function:

C#
public static ulong Factorial(ulong n)
{
    Func<ulong, ulong, ulong> factorial_acc = null;
    factorial_acc = (acc, x) =>
    {
        if (x <= 0) return acc;
        return factorial_acc(x * acc, x - 1UL);
    };

    return factorial_acc(1UL, n);
}

But the problem is, is that this won't work on a 32 bit machine.  Why you might ask?  Well, as Jomo Fisher, an F# team member, points out in his post about recursion in three languages, the C# compiler does not do tail call optimization.  Instead, all managed languages have a second opportunity to optimize through either NGEN or the JIT compiler.  As it turns out, the x64 version of the JIT will optimize the tail call right now, whereas the 32 bit compiler will not.  By default on your C# projects, it's targeted at AnyCPU which in my case since I'm running on a VPC, targets x86, will cause the overflow, whereas if I had been running on my base machine, it would have been ok.

This is another in the line of reasons why F# is the better language for recursion as well as functional programming fundamentals.  C# will get you partially there, but there are things such as these which will trip up developers and leaving themselves nowhere to go.  To which I say, pick the best language for the job at hand, whether it be F#, C#, Ruby, Python, Erlang, Haskell, JavaScript, etc.

Binary Recursion

Another form of recursion is binary recursion.  This form of recursion has the potential for calling itself twice instead of once as with before.  This is pretty useful in such scenarios as binary trees as well as the trite and overused Fibonacci sequence.  Such an example is below:

let rec fibonacci n =
  if n <= 2 then 1
  else fibonacci (n - 1) + fibonacci (n - 2)

But what's interesting is that we could do this better through tail recursion through the use of our inner auxiliary function, such as this:

let fib n =
  let rec fib_acc a b x =
    if x <= 1 then b
    else fib_acc b (a + b) (x - 1)
  fib_acc 0 1 n

But, back to the point, here's another use of binary recursion to print out the values in a tree.

type Tree<'a> = | Leaf of 'a | Node of Tree<'a> * Tree<'a>

let rec printBinaryTreeValues t =
     match t with
     | Leaf x -> printfn "%A" x
     | Node (l, r) ->
         printBinaryTreeValues l // called once
         printBinaryTreeValues r // called twice
 
printBinaryTreeValues (Node ((Node (Leaf "jeden", Leaf "dwa")), (Node (Leaf "trzy", Leaf "cztery"))))

As you can see from this example, I printed out the values from the binary tree, leaf by leaf using binary recursion. 

Wrapping It Up

As you can see here, I've covered a bit of ground on more recursive algorithms.  There is still yet more to be covered including processing of lists, unbalanced trees, continuations and so on.  More on that soon!  In the mean time, I hope you rediscover recursion and what it can do for you in terms of condensing code.  But, it's also important to see where it makes sense and where it doesn't.

kick it on DotNetKicks.com
Next week I will be presenting a lightning talk at the next Rockville .NET Users Group (RockNUG) on Functional C#.  I've talked about this topic quite a bit recently on a couple of threads now:
In this talk I hope to cover some of the basics including:
  • Currying
  • Filter
  • Map
  • Option Types
  • Tuples
  • Folding and Unfolding
  • Recursion
  • Lazy Evaluation
  • And more...
I know it's quite aggressive for such a short timeframe, and I may cut some of them due to how many questions I get.  This will be a continuation of my talk at the NoVA Code Camp back in May.  At the end, I hope to post my code at least.  I haven't published my slides due to me not being exactly happy with them yet.  I'll post a wrapup afterwards.

Anyhow, here are the details:

Date/Time:
Wednesday, July 9, 2008 - 6:30-9:00 PM

Location:
Montgomery College, Rockville
Humanities Building - Room 103
Rockville, MD
Directions

Hope to see you there!

kick it on DotNetKicks.com
Lately, I've noticed a few blogs are doing the come back to basics post, such as Scott Hanselman's.  I think that's an effort that should be lauded, because many have a tendency to go off on pretty advanced topics, yet not provide enough for those just starting to get the full context.  So, with that, I'm going to dig into one piece of programming that should have been ingrained should you have been a CS major in college or at least took some courses.

Recursion

Recursion is one of those basic topics that come up time and time again as one of the core topics.  It's not as often talked about in the imperative and object oriented world, but in the functional programming world, it matters a whole bunch.  Not only to be able to describe what it is, but the benefits, and where you might be able to break your problem down into using it.

So, before we get too far, what exactly is it, and how do we get there?  Basically recursion is to allow a function to call itself repeatedly during the body of that function.  Most imperative languages from the C world use for loops, while loops and so on, so many times recursion isn't needed.  Functional languages such as F#, allow for recursion, but makes them a special designation to enforce its behaviors. 

When coming to a place such as Microsoft, understanding these concepts are key.  Chances are you may get asked some of these questions, because people like me, when walking through complex structures, use recursive algorithms.  It's a nice tool to have in the toolbox.

Let's first look at an example not using recursion, then move towards recursion.  First start off with a pretty obvious one in computing a factorial:

public static int Factorial(int n)
{
    int result = 1;

    for (int i = 1; i <= n; i++)
        result *= i;

    return result;
}

To me, that's pretty horridly imperative.  We can make the code much more concise through the use of recursion and not mutate state during the operation.  With the proper attention of course to decrementing the counter so that we don't get into infinite loops.  An important lesson to learn is to not recurse infinitely, but in a controlled manner towards termination through a technique called "Well Founded Recursion".  Let's rewrite this to a much more manageable way:

C#
public static int Factorial(int n)
{
    if (n <= 1) // Exit condition
        return 1;

    return n * Factorial(n - 1);
}

F#
let rec factorial n =
  if n <= 1 then 1 else n * factorial (n - 1)

The basic idea is to break down your problems into smaller instances of the same problem.  Sometimes, the last thing we do in this function is return the recursion of the function.  This is called "Tail Recursion".  Below is a simple example of tail recursion:

let rec last l =
  match l with
  | [] -> invalid_arg "l"
  | [head] -> head
  | head :: tail -> last tail

The above sample will ultimately give me the last item in a list since F# has pointers to the head of the list, we need to get the last one.  We'll get more into tail recursion in the next post, but I thought I'd introduce it now.  It's very important as well to know.

Let's take another example, this time walking a directory structure to lazily display all files in a directory.  Of course we can note that the Directory.GetFiles already has the capability of getting all files in all folders, but for this exercise, let's pretend it doesn't exist.  How might we do this?  Let's try the lazy approach the first time around through IEnumerable<string>.

C#
public static IEnumerable<string> GetFiles(string rootPath)
{
    foreach(var file in Directory.GetFiles(rootPath))
        yield return file;
    foreach (var directory in Directory.GetDirectories(rootPath))
        foreach (var file in GetFiles(directory))
            yield return file;
}

F#
let rec getFiles rootFolder =
    seq {
          for file in Directory.GetFiles(rootFolder) -> file
          for dir in Directory.GetDirectories(rootFolder) ->> getFiles dir
        }

What this allows me to do is recurse through a given directory hierarchy by first grabbing all the files and then going through a tail recursion through all the subdirectories.  The F# way is a bit more concise through the use of Sequence Workflows, which I'll get to at another time.  If you understand anything about F#, understanding workflows is by far one of the most important.

Mututal Recursion

Another interesting way to express recursion is to define them simultaneously by separating the definitions.  This allows the functions to play off each other and work towards a deterministic end.  The cannonical example of this is the odd/even sample.  Let's walk through that quickly in both C# and F#:

C#
public static bool IsEven(int number)
{
    if (number == 0)
        return true;
    else
        return IsOdd(number - 1);
}

public static bool IsOdd(int number)
{
    if (number == 0)
        return false;
    else
        return IsEven(number - 1);
}

F#
let rec even n = (n = 0) || odd(n - 1)
  and odd n = (n <> 0) && even(n - 1)

As you can see, I decrement the counter each time and call the complement function until I reach 0 and at that point, I can determine which one hits 0 and then it becomes apparent whether it is odd or even.  F# goes a bit further to simplify this so that I can declare those functions as a pair through the use of the and keyword.  Parsing of trees is particularly useful using mutual recursion.  But just as easily, we could have done the above sequence without using recursion at all such as this:

public static bool IsEven(int number)
{
    return number % 2 == 0;
}

public static bool IsOdd(int number)
{
    return !IsEven(number);
}

So, that's not really a good example.  instead, let's look at a Hofstadter Male-Female Sequences instead. 

The pair of sequences is defined by Female(0) = 1 and Male(0) = 0 and
Female(n) = n - Male(Female(n - 1)
Male(n) = n - Female(Male(n - 1)

This is defined as the same way as before, but we recursively call in the Male to the Female and back to the male, whereas it's the opposite in the FemaleSequence method.  This is just a simple implementation of the above algorithm in our C# and F# code respectively.

C#
public static int MaleSequence(int n)
{
    if (0 == n)
        return 0;

    return n - FemaleSequence(MaleSequence(n - 1));
}

public static int FemaleSequence(int n)
{
    if (0 == n)
        return 1;

    return n - MaleSequence(FemaleSequence(n - 1));
}

F#
let rec maleSequence n = if n = 0 then 0 else n - femaleSequence(maleSequence n - 1)
  and femaleSequence n = if n = 0 then 1 else n - maleSequence(femaleSequence n - 1)

There are more examples out there, but in the mean time, there's plenty more to cover with recursion.  Next, let's cover lists.

Walking Lists

Another thing we can do with recursion is the ability to walk lists.  Most people think of lists as an ordered collection with a beginning and an end, with one right after another.  How terribly imperative if you think that way...  When we think of lists in a recursive way, we think of a list to have an item, and the rest of the list, or just an empty list.  C# and the base class library does not have this of course defined in any meaningful way, so let's look at the F# version of a list.  

module List =
  val hd: 'a list -> 'a
  val tl: 'a list -> 'a list

What that tells us is that the head item is the first, and the tail is the rest.  Let's do a quick recursion to calculate the length of a list using some pattern matching:

let rec length l =
  match l with
  | [] -> 0
  | head :: tail -> 1 + length tail

What I'm doing is pattern matching against the list.  What the pattern matching allows me to do is short circuit if it's an empty list, else, I'm splitting the head and the tail, and adding one to the length of the tail call with the tail of the list.  Let's look at another to concat a list of lists into a single list:

let listOfList = [[1;2;3;];[4;5;6;];[7;8;9]]

let rec concatList l =
  match l with
  | [] -> []
  | head :: tail -> head @ concatList tail

There is much more to cover here and I'll do that in subsequent posts.

Why Recursion?

Now that we covered some of the basics, it's time to ask the question why.  Why is recursion important and why use it?  Well, to answer that lightly, in order to walk such things as trees, directories, and so on, it's an absolute essential.  But, let's consider just a general functional programming question of why use it.

It's often the case that when people first start out with programming in F#, they are often in the imperative and OO mindset still.  That's ok since F# allows for this, but there are better ways to accomplish in the functional mindset.  Many times, people will port over their C# code directly to F# with the mutable variables or reference cells and leave an ugly mess.  Some of that can be solved with recursion.  Let's take a pretty naive example:

C#
public static void CountDown(int n)
{
    while(n > 0)
    {
        DoOperation();
        WaitSome();
        n -= 1;
    }
}

When translated directly to F# looks like this:
let countDown n =
  let mutable localN = n
  while localN > 0 do
    DoOperation()
    WaitSome()
    localN <- localN - 1

What we've had to do is use reference cells so that we could change the value of n directly.  From there, we have to use special operators to change and alias the value of n.  Not the prettiest code.  Not only that, but there is mutation galore, and I have to do extra work when creating a local iterating variable.  instead, we should focus on recursion and look at the problem slightly differently.

let rec countDown n =
    match n with
    | 0 -> 0
    | _ ->
      DoOperation()
      WaitSome()
      countDown (n - 1)

What this allows me to do is get rid of the local mutables, but also helps me break my problems into smaller, more similar problems which can be recursed.  This allows for better code reuse and maintenance I think.

Wrapping It Up

I'd like to spend some more time with tail recursion as I think it's pretty important.  As well, I'll also talk about how you can do the same in C# as well (with a little work of course).  But in the mean time, I hope I've demonstrated the simple act of recursion, when to look for it, and how to exploit it.  Not all problems are ready for recursion, but when you find the right opportunities for them, it's quite powerful.

kick it on DotNetKicks.com
Just a reminder about tonight's DC ALT.NET meeting.  I hope some from the FringeDC and the NoVA Language Group can make it out tonight as it's quite on topic.

The June meeting for DC ALT.NET has been set up for June 24th from 7-9PM.  Check out our site and our mailing list as more information becomes available.  Once again, I'd like to thank the Motley Fool for hosting the event as they did back in May.  This month, I'll be covering Applied Functional Programming with F# as a continuation of the talks I've been giving lately.  The beauty of this group like last month's topic of Lisp and this month's topic of F# is to step outside of the comfort zone, look to the outside for better approaches to doing things instead of the "What's new on MSDN" sessions that I hear about time and time again. 

The information is as follows:

Location:

Motley Fool
2000 Duke Street
Alexandria, VA, 22314
Map Link

Applied Functional Programming with F#

Recently, I've been doing quite a few talks on the introduction to functional programming and F#.  In this session, I hope to cover the basics in short order once again, but also to show the applicability of where this language really hits the right areas.  Are we going to immediately flock towards F# and abandon C# and other languages for all of your coding needs?  No, but we have definite areas where F# and functional programming excels. 

With this talk, I plan to focus on the following:
  • Functional Programming Basics
  • Custom Workflows
  • Async and Parallel Workflows
  • Mailbox Processing
  • Language Oriented Programming
  • Quotations and Expression Trees
  • Pattern Matching and Active Patterns
  • F# with MPI and High Performance Computing
Of course I also plan to spend some time talking about the latest piece which is FsTest, a Domain Specific Language for unit testing in F#.  This is an umbrella under a series of solutions for DSLs to describe how to test the behaviors of an F# application.

Going a Step Further

Since I've been covering concurrency with .NET lately, it has me thinking a bit about creating workshops to teach this on weekends.  Instead the way we see Code Camps, Day of .NET events and so on, it's an aggregate of all sorts of topics, and not going as deep as I wish.  So, instead, I'd like to rectify that situation to hold day long sessions to go deep in any given subject, much like they do with the Philly.NET group.  More on this as I flush out the details, but I'd like to hear feedback if people are interested in this sort of thing.

Hope to see a great turnout tonight!


kick it on DotNetKicks.com
Over the past couple of months, I've been working on some language oriented programming concepts in F# to prove how Domain Specific Languages could work.  This coincided with my lofty goals of working towards more Behavior Driven Development (BDD) frameworks and expressiveness that I think is needed to make them work.  So, this led me down a road of looking first at assertion syntax.  With that, it made me create FsTest which is now available on CodePlex.  This is meant to be a beginning of a conversation on doing better assertions through functional programming and language oriented programming and by no means an end to it.

The Starting Point

During some of my research, I came across a now defunct project on Google Code called FsUnit.  The idea behind this project was to put a syntactic wrapper around NUnit to make assertions a bit more readable and to use unique features to F# to make this happen.  To be able to write such things as this for my assertions seemed powerful:

1 |> should (equal 1)
true |> should (be True)

And so on...  Ultimately, I thought it could be improved and expanded, so I talked to the project owner, Ray Vernagus, and took some of the ideas and ran with them.

Looking at FsTest

I had a couple of issues with the code as it was.  But, overall the ideas were quite sound.  I also wanted to use xUnit.net as the back end testing framework as I'm pretty comfortable with it.  The support for tests on static classes, and the ability to assert on throwing exceptions were some of the pluses that made me move in this direction.  Listed below are some of the assertions that are supported:

Testing equality:
"foo" |> should equal "foo"
"foo" |> should not_equal "bar"
null |> should be Null
"foo" |> should be NonNull
"foobar" |> should contain "foo"
"foobar" |> should contain "hello"

Testing True/False:
true |> should be True
false |> should be False

Testing collections:
[1..10] |> should be NonEmpty
[] |> should be Empty
[1..10] |> should have 3
[1..10] |> should not_have 25

Exception Management:
throwsExceptionFunc |> should (throw_exception<ArgumentException>)
doesntThrowException |> should not_throw_exception

And some full examples might look like this:

#light

open System
open Xunit
open FsxUnit.Syntax

let throwsException() : unit =
  raise(ArgumentException "Bad things!")

[<Fact>]
let throwsException_should_throw_exception() =
  throwsException |> should (throw_exception<ArgumentException>)

[<Fact>]
let value_in_range_should_be_in_range() =
  2 |> should be_in_range 0 100

It's a work in progress, but I feel that I have most of the assertion cases covered in this DSL.  The ability through partially applied functions really shone when creating this library. 

The Concepts Behind It

What makes this work syntactically is the use of the forward operator.  I've covered this briefly in previous posts, but I'll elaborate on this again.  This is one of the most important operators available to us in the F# language.  This is the ability to invert the arguments in such a manner as the first argument is declared first and then the function statement is last.

This is how it's declared in F#:

let (|>) x f = f x

Which allows me to better express my intent and gives me the two advantages:
  • Clarity of intent - allows you to perform various operations on the data in a forward chaining way such as:
    let length = [1..10] |> List.map(fun x -> x * x * x) |> List.filter(fun x -> x % 3= 0) |> List.length

  • Type inference - allows type information to flow from the left to the right and allows me to express my data first as part of my method.  Shows better intention this way.

And similarly, something in C# would use extension methods to add a forward method, or maybe in this case a Should method which will then take a function as the second parameter, and then overload it as need be.

Using Actions might look something like this:

public static void Should<TArg1, TArg2>(this TArg1 arg1, Action<TArg1, TArg2> action, TArg2 arg2)
{
    action(arg1, arg2);
}

And conversely to use the Func delegate might look like this:

public static TResult Should<TArg1, TArg2, TResult>(this TArg1 arg1, Func<TArg1, TArg2, TResult> func, TArg2 arg2)
{
    return func(arg1, arg2);
}

public static void Contain<T>(IEnumerable<T> items, T item)
{
    Assert.Contains(item, items);
}

And to use it would look something like this:

Enumerable.Range(1, 10).Should(Contain, 3);

But, I'm not necessarily a fan of that style of syntax.  C# isn't quite a functional language which allows me to do such clever things with operators.  I don't think it will ever gain that level of expresiveness required.

Instead, I created two functions that could be chained together such as the should function and the be function.  This allows me to create some of the examples above.  Let's take a look at each function:

let should f actual =
  f actual

let be (f : 'a -> unit) actual =
  f actual

So, that will allow me to express such things as:

true |> should be True

And the true function might look something like this:

let True condition =
  Assert.True(condition)

As you can see, there isn't much to this, and covers most of the assertion scenarios that I've encountered.

Where To Go From Here?

So, where am I headed with this?  For right now, I'm happy with the assertion syntax I've been able to achieve with the language.  For the future, I'm looking at creating a more expressive way for doing BDD and the expresiveness of F# I think can handle such things.  I've been following the Specs project for a little bit and I think they have a few good things going for them, although Scala isn't a functional language in the way that F# is, so I'm not sure how many lessons can be applied.  Specter is another interesting one as well as Machine Specifications (MSpec) from the one and only Aaron Jensen.

I'd like to thank the F# team, Dustin Campbell and Harry Pierson (DevHawk) for their help on this as well.  The team around F# is a wonderful team and I can't wait for the CTP of F# to ship.  Makes me excited sometimes when working for Microsoft like I do.

But, in the mean time, feedback is appreciated.  Tell me what works and what doesn't.

kick it on DotNetKicks.com
I'm finally getting back to my concurrency in .NET posts after a brief hiatus after I got sidetracked on various other functional programming issues such as some language oriented programming and functional C#.  In the previous post, I talked about MPI in .NET and I need to get back to that one and address some of the issues raised in the next post in this series.  But, in the mean time, let's focus on another area of concurrency through message passing.  This time, let's bring Erlang into the picture.

Learning Erlang

For some of the upcoming talks about F# and functional programming I have planned, I will be putting a lot of the focus on concurrency programming through asynchronous and parallel mechanisms.  F# is not only a well established functional programming language, but its libraries also lend well to concurrency oriented programming.

But, with these adventures, I've always wanted to reach outside my comfort zone and learn from a language that had concurrency in mind from the start.  That made me turn my attention to Erlang.  Some of my motivations came from not only learning a new language, which is always nice, but also to learn some of the concurrency and messaging patterns so that I can apply that to F#. 

In my learnings, I've compiled a list of resources that can help you learn Erlang.  This includes books, podcasts and screencasts that you might find helpful for a good deep dive into the language.

Podcasts
Books
Screencasts
The Northern Virginia Ruby Users Group (NOVARUG) have recently started the NoVA Languages Group which has the goal of learning new languages to add to their toolbelt.  The group picked Erlang as the first language to learn and is currently going through the Programming Erlang book.  If you're in the DC area and interested in learning Erlang, I'd highly recommend that you join the group.

Erlang Style Message Passing in F#

To bring this back to F# for just a moment, we can apply some of the lessons learned from Erlang to F#.  Today we'll look at a simple kind of message processing called mailbox processing.  This pattern is popular in the Erlang language and has been applied to F#.  The mailbox is a message queue that you can scan for relevant messages to your message processing agents.  The MailboxProcessor class controls this action and is defined in the Microsoft.FSharp.Control.Mailboxes namespace.

This will be a simple example of how to send and receive messages using a single mailbox processor.  We can define a message type that will encompass our data.  This usually comprises of a receive, a send, and a stop.  But, the great thing is that it's not limited to that.  We'll use async workflows (yet another post I need to do) to process the message itself, hence why you see the return! statements in there.

#light

open Microsoft.FSharp.Control.CommonExtensions
open Microsoft.FSharp.Control.Mailboxes

type Message = Phrase of string | Fetch of IChannel<string> | Stop

type MessageAgent() =
  let agent = MailboxProcessor.Start(fun inbox ->
    let rec loop (phrase : string) =
      async {
              let! msg = inbox.Receive()
              match msg with
              | Phrase item ->
                return! loop(item)
              | Stop ->
                return ()
              | Fetch replyChannel ->
                do replyChannel.Post(phrase)
                return! loop(phrase)
            }
    loop(string.Empty)
  )
 
   member x.Send(s) = agent.Post(Phrase(s))
   member x.Stop() = agent.Post(Stop)
   member x.Fetch() = agent.PostAndReply(fun replyChannel -> Fetch(replyChannel))

What I'm also doing is defining handling logic for each message type, the stop, the fetch and the receiving of a phrase.  If it's a stop message, we return immediately, else if it's a phrase, we'll go ahead and loop on the given item, and once we're ready to fetch it, then we'll go ahead and post it.  After that, I'll go ahead and expose three methods which will allow external access to sending and receiving messages, as well as stopping.

Now, that I have done the basic plumbing for handling messaging like this, then we can go ahead and start sending messages to it and receiving.

let agent = new MessageAgent()

agent.Send("Hello world!")
let result1 = agent.Fetch()
printfn "Message result1: %s" result1

agent.Send("Hello world again!")
let result2 = agent.Fetch()
printfn "Message result2: %s" result2

This is a pretty naive example and just to show the basics of how it could be used.  I'm not going to pretend it's anywhere near as powerful as the Erlang message passing libraries, but it's a pretty good start.

Robert Pickering has a pretty good example of Erlang style message passing here.  F# also comes with a sample using the MailboxProcessor for a program called ConcurrentLife which is located in the Samples directory of your F# installation.

Retlang

But, what about those in the C# world?  Is there a solution?  An interesting piece that came up during the look into concurrency in .NET led me back to Retlang, which is a takeoff on Erlang by the author Mike Rettig.  I had looked at this previously when Ayende did a brief look back in November here.  The framework itself borrows a few ideas from Scala in terms of the event-based actors.  Let's take a look at a quick example of sending and receiving messages on it.

static void Main(string[] args)
{

    IProcessContextFactory factory = new ProcessContextFactory();
    factory.Start();
    IProcessContext publisher = factory.CreateAndStart();
    IProcessContext consumer = factory.Create();

    consumer.Subscribe<int>(new TopicEquals("retlang"), (header, id) => Console.WriteLine(id));
    consumer.Start();

    for (int i = 0; i < 25; i++)
        publisher.Publish("retlang", i);
   
    publisher.Stop();
    publisher.Join();

    consumer.Stop();
    consumer.Join();
   
    factory.Stop();
}

Looks to be a well designed architecture and I'll continue to look at this a bit further.  In the mean time, check out Mike's blog for more information.

Wrapping It Up

I hope this brief introduction will get you excited about looking at other functional programming languages and what they can do for you.  If not to learn them and apply them in your daily work, but to understand the concepts behind them is rather powerful and should be part of your learning plan anyhow.  These above examples of implementations in other languages should help you along.

But, what do I think of Erlang overall?  It's too early to tell.  Even though the language has been around for a while, it has seen a recent resurgance.  With the Pragmatic Programmers throwing their weight behind the language, who's to say?  But I think it'd take a bit to push this as yet another language with a virtual machine into the mix over say the JVM, CLR, and so on.  Instead, maybe we'll see an implementation on top of .NET, or just applying the lessons learned from it such as we've seen in F#.  It was funny to listen to Joe Armstrong think pretty much the same thing as maybe the language won't take off, but the ideas can and ultimately will.

kick it on DotNetKicks.com
In the last installment of Functional C#, I covered a bit about creating delayed evaluation lists based upon unfolding constructs and generating lists from external resources.  Those are some of the higher level high order functions you can do in C#, but let's look at a few more, plus those that are already available to us in .NET 3.5.  We'll take samples from F# and show that some of them already exist in the base class libraries and you can use them today without having to reinvent any wheels.

Iterators

One of the most simple operations in functional programming is just the simple iterator.  In F#, we have two ways of iterating through any given list or sequence, by index or through the enumerator.  Below are simple examples in F# doing just that:

[1..10] |> List.iteri(fun i j -> printfn "%d - %d" i j) // By index
[1..10] |> List.iter(fun i -> printfn "%d" i) // By enumerator

If we inspect the List<T> class and the Array class, we'd find that we have methods that does the enumerating by using the enumerator that looks like this:

List<T>:
void ForEach<T>(Action<T> action);

Array:
static void ForEach<T>(T[] array, Action<T> action);

So, that I could iterate a list much like this:

Enumerable.Range(1, 10).ToList().ForEach(Console.WriteLine);

One thing I thought was critically missing from the IEnumerable<T> class was the ForEach or Iterate as the F#'er in me would say.  Implementing one is pretty simple using the C# 3.0 techniques:

public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
{
    if (items == null)
        throw new ArgumentNullException("items");
    if (action == null)
        throw new ArgumentNullException("action");

    foreach (var item in items)
        action(item);
}

But, what about by index?  IEnumerable<T> has no such concept of this, so it doesn't really do us any good to try.  But, it works very nicely with IList<T> or System.Array.  Let's do both just for grins.

public static void IterateIndex<T>(this T[] items, Action<int, T> action)
{
    if (items == null)
        throw new ArgumentNullException("items");
    if (action == null)
        throw new ArgumentNullException("action");

    int lower = items.GetLowerBound(0);
    int upper = items.GetUpperBound(0);
    for (int idx = lower; idx < upper; idx++)
        action(idx, items[idx]);
}

public static void IterateIndex<T>(this IList<T> items, Action<int, T> action)
{
    if (items == null)
        throw new ArgumentNullException("items");
    if (action == null)
        throw new ArgumentNullException("action");

    for (int idx = 0; idx < items.Count; idx++)
        action(idx, items[idx]);
}

And then we can do a simple iteration this way:

Enumerable.Range(1, 10).ToArray().IterateIndex((i, j) => Console.WriteLine("{0}-{1}", i, j));

Like I said, nothing too complex here.  But, let's get more advanced, shall we?

Folding

In the functional programming world, the fold, also known as reduce and accumulate, is a family of high order functions that process a data structure in a given order and build up a return value.  The data structure processed in this type of function is usually a list of some sort.  They are pretty powerful which help you avoid a lot of looping and recursion in your code.  These functions are in direct contrast to the unfold function that I talked about last time which took a function to generate a list.

In the F# world, we have a couple of ways about thinking about folds, we have support for both left and right folds.  The Seq.fold function is applied from a left to right fashion as is the List.fold_left function.  The List.fold_right function is the inverse which applies from the right to left.  The Seq does not have this as it is a forward only evaluated collection much like IEnumerable<T>. 

Some basic examples in F# would look like this:

[1..10] |> Seq.fold(fun acc x -> acc + x) 1
[1..10] |> List.fold_left (fun acc x -> acc + x) 1

Or using simple operator function such as sum would look like this:

[1..10] |> Seq.fold (+) 1

Where the basic structure is:
val fold_left : ('b -> 'a -> 'b) -> 'b -> 'a list -> 'b

The fold right example would look something like this:

List.fold_right max [3; 5; 7; 9; 1; 2;] System.Int32.MinValue

Where we'd want to get the maximum value from our given list, and the Int32.MinValue is our accumulator.

Where the basic structure is:
val fold_right : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b

So, how would we implement this in C#?  Let's do a simple implementation of fold (or fold_left) in C#.  It's a rather easy implementation.  Let's first do one for IEnumerable<T> and then for IList<T>:

public static T Fold<T, U>(this IEnumerable<U> list, Func<T, U, T> func, T acc)
{
    foreach(var item in list)
        acc = func(acc, item);

    return acc;
}

Enumerable.Range(1, 10).Fold((acc, x) => acc + x, 1);

And now for the IList<T> would look something like this:

public static T FoldLeft<T, U>(this IList<U> list, Func<T, U, T> func, T acc)
{
    for (int index = 0; index < list.Count; index++)
        acc = func(acc, list[index]);

    return acc;
}

new List<int> { 1, 2, 3, 4, 5 }.FoldLeft((acc, x) => acc + x, 1);

Seems simple enough where we keep executing the function with the accumulator and the current item in the collection until we are complete.  The same implementation can also be said for the FoldRight function which does the reverse of

public static T FoldRight<T, U>(this IList<U> list, Func<T, U, T> func, T acc)
{
    for (int index = list.Count - 1; index >= 0; index--)
        acc = func(acc, list[index]);

    return acc;
}

int foldRight = new List<int> { 5,9,1,3,4,3 }.FoldRight((acc, x) => Math.Max(acc, x), int.MinValue);

Where the answer would be 9 as the maximum number.

But, wait!  Don't we already have these things in .NET 3.5?  With LINQ, the answer is yes, well, partly.  Let's look at the Enumerable.Aggregate function:

public static TAccumulate Aggregate<TSource, TAccumulate>(
  this IEnumerable<TSource> source,
  TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func)

And an implementation might look like this:

Enumerable.Range(1, 10).Aggregate(1, (acc, x) => acc + x);

So, as you can see, it solves the Fold and FoldLeft operations, but does not cover the case of FoldRight, unless I were to somehow use the Reverse extension method also available on Enumerable class.  But of course that could be bad if my collection were infinite...

Filtering

Another idea from F# is the filter function.  This gives us the ability to filter a given collection by the criteria given.  The function looks like this:

val filter : ('a -> bool) -> 'a list -> 'a list

And a simple implementation would look like this:

let mod3List = [1..100] |> List.filter(fun x -> x % 3 = 0)

But, how would we do something similar.  Let's mock something up in C# using extension methods.

public static IEnumerable<T> Filter<T>(this IEnumerable<T> source, Predicate<T> func)
{
    foreach (var item in source)
    {
        if(func(item))
            yield return item;
    }
}

Enumerable.Range(1, 100).Filter(x => x % 3 == 0);

That's great, but...  we already have that in .NET 3.5 once again through LINQ, so no need to reinvent the wheel here either.  Instead, let's look at the Enumerable.Where method:

public static IEnumerable<TSource> Where<TSource>(
  this IEnumerable<TSource> source,
  Func<TSource, bool> predicate)

And then I can write my function as follows:

Enumerable.Range(1, 100).Where(x => x % 3 == 0);

So, once again, the core libraries have a lot of the functional programming things in mind already that we can take advantage of.  Let's look at one final one for this installment.

Mapping

The map function in functional programming is to take a function and apply it to each item in the collection.  The function will look like this:

val map : ('a -> 'b) -> 'a list -> 'b list

let cubeList = [1..100] |> List.map(fun x -> x * x * x)

Now, let's prototype how this might look in the C# world to implement something like this:

public static IEnumerable<TResult> Map<T, TResult>(this IEnumerable<T> source, Func<T, TResult> func)
{
    foreach (var item in source)
        yield return func(item);
}

var cubes = Enumerable.Range(1, 100).Map(x => x * x * x);

But once again, .NET 3.5 and LINQ already solved this problem as well.  The Enumerable.Select method already implements this very logic:

public static IEnumerable<TResult> Select<TSource, TResult>(
  this IEnumerable<TSource> source,
  Func<TSource, TResult> selector)

This allows me to rewrite my function so that it looks like this:

var cubes = Enumerable.Range(1, 100).Select(x => x * x * x);

Wrapping It Up

As I gave examples above, many of the functional programming functions that are a given in the FP world have already been implemented in .NET 3.5, albeit under different names.  Once again, I'll emphasize that C# is an "ok" language to understand functional programming aspects and let's you get over one hop of the programming style without having to learn the new language.  But, to get the full breadth of what functional programming can do for you in terms of pattern matching, type inference, generic functions, immutability and so on, F# is the real option in the .NET world. 

kick it on DotNetKicks.com
In a previous post, I talked about how I thought C# has some significant drawbacks from being considered a more functional language.  But, that wasn't to exclude it as a language altogether, as it has some pretty useful features.  Lately, when I have been talking about F# in my sessions, many people wonder why use F# and not just use the functional aspects of C#.  They figure that sooner rather than later, C# will have most of those features anyways.  That may be true to some extent, but C# will never be as expressive, nor flexible as F# as a functional language or for doing language oriented programming.  My thinking is that C# should remain the mainstream OOP and slightly FP language that it is without trying to do too much at once such as becoming fully dynamic or fully functional.

But, that's not to stop us from having fun with trying to implement some of the features of the F# libraries in C#.  In fact, that helps you gain the understanding of what it may be doing underneath the covers.  I find it's easier sometimes to teach the fundamentals of functional programming through the use of C# for those in the imperative and OO mindset.  Today, let's walk through a few samples of creating lists through functional programming techniques.  But first, a correction.

Corrected on Currying

As I mentioned before, currying is the simple act of reducing a function with multiple arguments into a single argument high order function.  I posted the typical example using the Func, but wasn't quite right on the Action one.  My thanks to Alexey Romanov for pointing this out. 

public static Func<TArg1, Action<TArg2>> Curry<TArg1, TArg2>(this Action<TArg1, TArg2> action)
{
    return x1 => x2 => action(x1, x2);
}

Now with both of these currying becomes a bit more natural so that now I can do the following:

Action<IEnumerable<int>, int> contains = (x, y) => x.ShouldContain(y);
var curriedContains = contains.Curry();
var containedBy30 = curriedContains(Enumerable.Range(1, 30));
containedBy30(3);

Still, it's not as natural as I'd like it to be in terms of having to specifically mark is as a curried function instead of it being more natural.

Generating Lists Through Unfolding

This is probably one of the more difficult functions to understand.  The goal of the unfold function is to create a delayed evaluation list.  What we want is the ability to have a function evaluated repeatedly so that we can generate the items in our list.  This evaluation function must return an option type (Option<A>) that can be either Some(x) or None as well as the need for tuples (Tuple<TArg1, TArg2>).  But, in C#, we don't have those features.  After consulting Dustin Campbell, I noticed that he already has done a bit of legwork in post his post "Apples and Oranges" to compare the performance of a C# and F# solution to a Project Euler problem.  Let's look at a sample F# unfolding sample of creating an infinite Fibonacci sequence and then we'll look at how to implement in C#.

let fibs = Seq.unfold (fun (i,j) -> Some(i,(j,i+j))) (1,1)

What we obviously need are the Option<T> and Tuple<TArg1, TArg2> type which Dustin has provided in the download link for the post.  I modified mine slightly to follow F# naming conventions for the Tuple to have Item1, Item2 and so on.  Now, let's look at the actual implementation of the Unfold function.  This function will start with the start value and then calculate the next value from the generator, yield the first item and reset the next item.  This will keep on evaluating until the option type returns None which is equivalent to null in the imperative world.

public static IEnumerable<TResult> Unfold<T, TResult>(Func<T, Option<Tuple<TResult, T>>> generator, T start)
{
    var next = start;

    while (true)
    {
        var res = generator(next);
        if (res.IsNone)
            yield break;

        yield return res.Value.Item1;

        next = res.Value.Item2;
    }
}

Now we can define the actual implementation.  Let's go ahead and create that implementation using the Unfold function.  As you can see, we're pretty much mirroring the F# code snippet word for word.  As you may notice, the C# is a bit more verbose because it requires us to physically declare tuples.

var fibs= Unfold(x => Option.Some(Tuple.New(x.Item1, Tuple.New(x.Item2, x.Item1 + x.Item2))), Tuple.New(1, 1));
var first20 = results.Take(20);

Of course I could have done this without the Unfold function and just had a property with an infinite sequence, but I like this solution because it's quite nice and generic.  Something like this though could have sufficed:

public static IEnumerable<long> Fibonacci
{
    get
    {
        long i = 1;
        long j = 1;
        while (true)
        {
            yield return i;
            long temp = i;
            i = j;
            j = j + temp;
        }
    }
}

But, then that leaves off the implementing the following section in a nice generic fashion.

Initializing Lists

Now that we understand the unfolding function, we can apply it to any number of operations.  How about if we want to initialize a delayed infinite or finite list?  We can reuse the logic from the Unfold to make this happen.  First, let's look at some F# samples of how we might want to do this:

let allCubes = Seq.init_infinite (fun x -> x * x)
let tenCubes = Seq.init_finite 10 (fun x -> x * x)

The first example, I created a list of all cubes possible, and the second, I created a list of just the first 10.  You must create a function which passes in the current iteration, and then you can use it to calculate your return value.  Ok, so, how would we do this in C#?  Well, let's just first put up the infinite list because it's easier to implement.

public static IEnumerable<T> InitializeInfinite<T>(Func<int, T> f)
{
    return Unfold<int, T>(s => Option.Some(Tuple.New(f(s), s + 1)), 0);
}

What I'm doing is return an option type with a tuple of the current value and the next value.  Then I seed the function with the starting value of 0.  My function that I create is passing in the current index, so that I can increment it.  That's pretty easy, but how about limiting it to an exact number of items to take?

public static IEnumerable<T> InitializeFinite<T>(int count, Func<int, T> f)
{
    return Unfold<int, T>(s =>
    {
        if(s < count)
            return Option.Some(Tuple.New(f(s), s + 1));
        else
            return Option<Tuple<T, int>>.None;
    }, 0);
}

Now, what I've done is made sure that my index is less than the count specified and then return the next value, or else I return none.  With this in place, I can then create the exact duplicates from F# from above:

var allCubes = InitializeInfinite(x => x * x);
var tenCubes = InitializeFinite(10, x => x * x);

Generating Lists

So, in the previous example, we have a way to create a nice list through an initialize function.  But what if we want to create a list from some sort of cursor?  This cursor could be anything from a stream, to a reader, etc.  In F#, we have the generate and generate_using functions which allow us to specify the opening cursor function, the actual computation and then a closing cursor function.  Let's look at a quick sample:

let htmlList = Seq.generate (fun () ->
  File.OpenText(@"D:\Tools\Reflector\ReadMe.htm")) (fun x ->
    if x.EndOfStream then None else Some(x.ReadLine())) (fun x ->
      x.Close())

Here I am generating a list of text from a text file, while specifying the opening, calculating of the list, and then the closing.  But, since my reader that I'm using supports IDisposable, let's go ahead and use the generate_using, which was created for just that.

let htmlList = Seq.generate_using (fun () ->
  File.OpenText(@"D:\Tools\Reflector\ReadMe.htm")) (fun x ->
    if x.EndOfStream then None else Some(x.ReadLine()))

There!  A bit better.  But, how do we do this in C#?  Well, let's go ahead and use the Option type that we took from above.  From there, we can pretty much go line for line with the F# version.  What we want to do is continue executing until we get a None return value and at that point, clean up and then exit.

public static IEnumerable<TResult> Generate<T, TResult>(Func<T> opener, Func<T, Option<TResult>> generator, Action<T> closer)
{
    T openerResult = opener();

    while (true)
    {
        var res = generator(openerResult);
        if (res.IsNone)
        {
            closer(openerResult);
            yield break;
        }

        yield return res.Value;
    }
}

Now that we have that defined, we can create the GenerateUsing function in much the same way by calling Generate and passing in a function that disposes of the resource.

public static IEnumerable<TResult> GenerateUsing<T, TResult>(Func<T> opener, Func<T, Option<TResult>> generator) where T : IDisposable
{
    return Generate(opener, generator, x => x.Dispose());
}

Now, I have the ability to read in files nicely into my lists just by doing this with both the regular generate and generate_using:

var generatedResults = Generate(() => File.OpenText(@"D:\Tools\Reflector\ReadMe.htm"), x =>
    {
        if (x.EndOfStream)
            return Option<string>.None;
        else
            return Option.Some(x.ReadLine());
    }, x => x.Dispose());
generatedResults.ForEach(Console.WriteLine);

var generatedUsingResults = GenerateUsing(() => File.OpenText(@"D:\Tools\Reflector\ReadMe.htm"), x =>
{
    if (x.EndOfStream)
        return Option<string>.None;
    else
        return Option.Some(x.ReadLine());
});
generatedUsingResults.ForEach(Console.WriteLine);

The GenerateUsing function is a bit cleaner if we can help it, so that we don't have to worry about a function that closes the resource and instead, we could make a better assumption about the type and close it ourselves generically.

Wrapping It Up

As you can see, you can implement plenty using C# and its current constructs, especially in regards to creating lists.  Is it the better approach over F#?  No, but it will get you most of the way there as I have shown here with the addition of some more types which include the Tuple<TArg1, TArg2> and the Option<T> type. 

The more important thing for you to remember is the bare concepts that this post covered.  There are plenty more to learn in terms of recursion (tail, mutual, etc), folding and so on.  But, ultimately, I need to get back and focused on my concurrency in F# posts, unless there is a big demand for more of these functional C# samples.

kick it on DotNetKicks.com
With great excitement, Portland State University and the University of Chicago has announced the 11th annual International Conference on Functional Programming (ICFP) Programming Contest to be held from July 11th to July 14th, 2008.  If you're not familiar with the contest, it is one of the most advanced and prestigious programming contests. It is a good chance to show off your programming skills, your favorite languages and tools, and your ability to work as a team as you tackle these hard problems. What's great about this is that you can have a team consisting of one or more people, from any part of the world, and with any programming language you so choose.  The contest will begin at noon (PDT) on July 11th and all entries must be received by the organizers by July 14th at noon (PDT). 

To me, this is a much more interesting than the International Obfuscated C Contest because we're actually trying to solve a problem, let alone in any language we so choose.

Years Past

In years past, there have been some pretty interesting contests.  Last year's contest was an interesting one to help find a DNA prefix to help an alien, who was dropped onto Earth from an interstellar garbage collector, survive with the new climate.  And the year before that was to analyze an ancient codex and universal machine.  The list of previous contests can be found at the contest site.

This time around, it'd be great to see a few teams submit with F#!

kick it on DotNetKicks.com
The June meeting for DC ALT.NET has been set up for June 24th from 7-9PM.  Check out our site and our mailing list as more information becomes available.  Once again, I'd like to thank the Motley Fool for hosting the event as they did back in May.  This month, I'll be covering Applied Functional Programming with F# as a continuation of the talks I've been giving lately.  The beauty of this group like last month's topic of Lisp and this month's topic of F# is to step outside of the comfort zone, look to the outside for better approaches to doing things instead of the "What's new on MSDN" sessions. 

The information is as follows:

Location:

Motley Fool
2000 Duke Street
Alexandria, VA, 22314
Map Link

Applied Functional Programming with F#

Recently, I've been doing quite a few talks on the introduction to functional programming and F#.  In this session, I hope to cover the basics in short order once again, but also to show the applicability of where this language really hits the right areas.  Are we going to immediately flock towards F# and abandon C# and other languages for all of your coding needs?  No, but we have definite areas where F# and functional programming excels.  Some of these include asynchronous programming, parallel programming, metaprogramming, language oriented programming, messaging systems and so on.  The list could go on and on.  We'll explore some of the functional programming basics and some of these scenarios as we try to wrap our heads around this emerging, yet not new paradigm. 

Be there and hope to see a great crowd!

kick it on DotNetKicks.com
More Posts Next page »