# Monday, October 19, 2009

Most every language has a sweet spot, something it does better than any other language around it.

C and C++ are really good for writing console video games because of its speed, portability, and deterministic finalization.  Perl is really good at string manipulation. Java has a gazillion OSS frameworks and application servers. Ruby is damn pretty and has a couple of really super productive web frameworks.

Erlang has a very succinct and elegant syntax, a syntax that is highly optimized for writing recursive functions. One could argue that’s just a property of being a functional language, but I think there’s more too it than just being functional.

C# 3 has a decent functional syntax that is clearly more succinct than Java or C# 2, but it just doesn’t stack up to Erlang.

Warning: don’t use these code samples in production code! They use Ω(n) extra storage space and are toy functions.

For example, take this Erlang quick sort function:

qsort([]) -> [];
qsort([Pivot|T]) ->
	qsort([X || X <- T, X < Pivot])
	++ [Pivot] ++
	qsort([X || X <- T, X >= Pivot]).

That is, IMO, beautiful code.  Small, compact, succinct, powerful, readable. On the other hand, what does a comparable implementation look like in C# 3? 

static IEnumerable<int> Qsort(IEnumerable<int> list)
{
    if (list.Count() <= 1)
        return list;

    int pivot = list.First();
    var tail = list.Skip(1).Take(list.Count() - 1);

    var result = new List<int>();
    result.AddRange(Qsort(tail.Where(o => o < pivot)));
    result.Add(pivot);
    result.AddRange(Qsort(tail.Where(o => o >= pivot)));
    return result;
}

It works, it doesn’t contain any loops and only one if statement (the initial C# 2 version was even longer and uglier), but its still very much lacking in the beautiful code department.  Did you also notice it was more than twice as long as the Erlang function? Perhaps you can improve the syntax?

I don’t have numbers to prove this, but the Erlang implementation can also be made faster with a lot less effort since each recursive call can be run in parallel.  Try that in C# without writing extra threading code. Even if you did, your threads are probably going to be a lot slower than the light weight threads in Erlang.

Recursive functions in Erlang are definitely a sweet spot.

Monday, October 19, 2009 3:52:56 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [1]  | 
# Friday, October 09, 2009

Sometimes you need a paradigm shift, a different way to look at the same set of problems. Erlang I hope will give me that paradigm shift. Hopefully I can add another a screwdriver to my toolbox, if not, hopefully something besides a hammer.

I just bought a copy (on PDF) of Programming Erlang Software for a Concurrent World. I’ve only gotten through the first few pages of chapter 1, and so far I’m impressed. I guess its no surprise as the book is published by Pragmatic Programmers.

I finally understand why mathematicians like functional languages.  Its so obvious now, and it has a lot to do with immutability. From the book:

When I went to school, my math teacher said, “If there’s an X in several different parts in the same equation, then all the Xs mean the same thing.” That’s how we can solve equations: if we know that X+Y=10 and X-Y=2, then X will be 6 and Y will be 4 in both equations. But when I learned my first programming language, we were shown stuff like this:
X = X + 1
Everyone protested, saying “you can’t do that!” But the teacher said we were wrong, and we had to unlearn what we learned in math class. X isn’t a math variable: it’s like a pigeon hole/little box.... In Erlang, variables are just like they are inmath.When you associate a value with a variable, you’re making an assertion—a statement of fact. This variable has that value. And that’s that.

I guess mutability is a strange beast for most people when learning to program, forcing a lot of them to quit while in their first year of CS.  Well, that and pointers. Its an odd way to think of things for people living in a concrete world. I wonder if first year CS students wouldn’t have an easier time learning a functional language?

Friday, October 09, 2009 1:09:12 AM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [0]  | 
# Thursday, October 01, 2009

I found another reason to love open source NUnit over MSTest, NUnitEx. NUnitEx provides a fluent DSL to write NUnit assertions where Visual Studio intellisense is your guiding friend. I’m sure you’re thinking, “Yeah whatever, show me some code.”

[Test]
public void AdjacencyGraph_is_directed_by_default()
{
    var graph = new AdjacencyGraph<Task, TaskDependency>(DoNotAllowParallelEdges);
    graph.IsDirected.Should().Be.True();
}

Nice eh? graph.IsDirected is the property I want to test.  Everything after that is the NUnitEx extension methods. I know I find it much more readable than this:

[Test]
public void AdjacencyGraph_is_directed_by_default()
{
    var graph = new AdjacencyGraph<Task, TaskDependency>(DoNotAllowParallelEdges);
    Assert.IsTrue(graph.IsDirected);
}

I would also like to add that this syntax is likely to be included in NUnit 3, which would be excellent!

Thursday, October 01, 2009 1:19:34 AM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [1]  |