Home > Programming > Haskell and Functional Programming

Haskell and Functional Programming

March 28, 2009

So for some reason I’ve decided to dive into functional programming as I never really learned much about it other than scratching the surface at college with Lisp. I’ve started off with Haskell and have been going through the book Real World Haskell. It’s a decent book, but the best part is the online version is free. =)

Initially there were some cool things, like pattern matching for methods, but I’m starting to lose interest… I’m having a real issue wrapping my head around the real world benefits of using Haskell (or any functional language). I mean, the syntax of the language is nice, some of the concepts are interesting, but I don’t know, maybe I’m missing something.

Side note: I originally picked Haskell because I knew it ran fine on both Windows and OS X. However, I just did some research and found out that F# works with Mono on OS X, so I might see about testing the waters over there.

Well… off to check out an F# presentation to see if it can convince me. 😉 http://channel9.msdn.com/pdc2008/TL11/

Categories: Programming
  1. Benny
    March 28, 2009 at 11:11 am

    The benefit that I read about most often for functional programming is parallelism. Because you don’t have to worry about state and side-effects, it becomes trivially easy to split up your computations across a large number of machines. Google’s MapReduce is the poster-child for reaping massive parallel computing benefits via functional programming.


  2. March 28, 2009 at 12:24 pm

    Yeah… I know about the parallelism piece, though, I’m struggling on how that is going to help me with my real problems to solve.

    For example, if I were to right an automation library for a program, what is FP going to do for me? I’ll have few things that I can do in parallel there as most transitions will require a serial approach.

  3. March 28, 2009 at 12:58 pm

    Let me rephrase actually…

    Some of the benefits I get, like being able to eliminate a certain class of bugs.

    C# code –

    int SumList(List<int> list) {
      int sum = 0;
      foreach (int i in list) {
        sum = sum + i;
      return sum;

    F# code –

    let sumList nums =
      nums |> Seq.sum

    Though, this is arguably a cop-out as we are calling a function to handle the sum for us. We could have done this in C#.

    C# code –

    // Our library that is defined somewhere.
    public class Seq {
      static int Sum(List<int> list) {
        // sum the list, i.e. with the code above
    // Our real program.
    int sum = Seq.Sum(myList);

    Or even better, use extension methods

    public static class FunctionalGoodness {
      public static int Sum(this List<int> list) {
        // sum it up
    // Then we just do this:
    List<int> list = // some list.
    int sum = list.Sum();

    I guess my point is, I see no benefit of the C# implementation the Sum method vs. the F# method.

    Now, I can somewhat see the value of exposing functional APIs to use in your programs. Clearly your code is cleaner when you don’t have to write the implementation the sum. But it seems to me that you always get down to the nitty-gritty of having to write the implementation of ‘Sum’ in a more imperative way.

  4. Benny
    March 28, 2009 at 7:01 pm

    Why would you have to write the implementation in ‘Sum’ in a more imperative way?

    In F#, you should be able to do this:

    let rec sum list =
    match list with
    | h::tail -> (sum tail) + h
    | [] -> 0

    (Source: http://tomasp.net/articles/fsharp-ii-functional.aspx)

    In C#, you can accomplish the same via delegates:

    int sum1(List list) {
    return list.Aggregate(
    delegate(int currentSum, int num) {
    return currentSum + num;

    Or lambda calculus (seems to require System.Linq):

    int sum(List list) {
    return list.Aggregate(
    (currentSum, num) => currentSum + num

    (Source: http://visualstudiomagazine.com/columns/article.aspx?editorialsid=2505)

    The following article discusses functional programming in C# and F#, and showcases how easy it is to parallelize functional methods via C#’s Parallel Extensions (currently available as CTP, will be an official part of .NET 4.0):


    For another perspective on the usefulness of functional programming in the real world, you might be interested in Paul Graham’s essays. The dude (& friends) built an online website-builder called Viaweb in LISP, which became successful enough that it was later bought by Yahoo! for about $45 million, and is still operational today as Yahoo! Store. This is a good one to start with:


    The rest are here: http://www.paulgraham.com/articles.html

    (FYI, w.r.t. programming, the interesting essays are the earlier ones at the bottom. The newer essays are almost all about start-ups)

    Lastly, have you checked out projecteuler.net before? It’s a website that hosts an ongoing series of math/programming challenges. I’ve found it useful as a way to practice C++/C# as I try to learn them. I think it could be a great way to experiment with functional programming as well.

  5. March 28, 2009 at 7:31 pm

    For sum, yes, you can do it via recursion, though you pay a penalty for doing so, mainly a stack overflow problem. You can get around this by creating another function that you call so you get into a tail-recursion which, in theory, should avoid the overflow.

    What does this mean? Every time you iterate you MUST use tail-recursion or you face the problem of stack overflows. Ok.. but now this makes writing the library methods more difficult.

    The other way, and the way it’s done in the F# source code, is to iterate over the list with an enumerator, or basically a for-loop. This avoids the cost of recursion and the possibility of a stack overflow.

    I’ll check out the articles.

    As for projecteuler.net, I find that FP is extremely well oriented to mathematical problem solving… with that in mind, that doesn’t seem to be a good way to see how FP works in “wild”. Very little of my programming tasks revolve around solving mathematical problems.

    I’m going to take a look at F# some more, once I stop fiddling with trying to install it via macports. 😉

  1. No trackbacks yet.
Comments are closed.
%d bloggers like this: