[Haskell] Haskell in industry?

Nick Benton nick at microsoft.com
Wed Sep 15 06:21:39 EDT 2004


Haskell and ML certainly have advantages over Java and C#. But it's important to realise firstly that they have disadvantages too, and secondly that the imperative/OO languages are evolving fast. When the mainstream was only C/C++, the most important advantage of functional languages was garbage collection. Once Java and C# became popular, polymorphism became the "unique selling proposition". But now both Java and C# have generics and anonymous functions. For example, in C# 2.0 the most obvious translation of map is just

static IEnumerable<T2> map<T1,T2>(IEnumerable<T1> arg, Converter<T1,T2> f) {
            foreach (T1 x in arg)
                yield return f (x);
        }

this is type-safe, takes any type of collection as input and returns a lazily-evaluated collection of results. 

Or we could have

        static S2 map<T1, T2, S1, S2>(S1 arg, Converter<T1, T2> f)
        where S1:IEnumerable<T1>
        where S2:ICollection<T2>,new()
        {
            S2 result = new S2();
            foreach (T1 x in arg)
                result.Add(f(x));
            return result;
        }
Which is parameterized over both the element types *and* both the collection types. You can use it like this:

    int[] myints = { 1, 2, 3 };
    List<string> mystrings =      
        map<int,string,int[],List<string>>(
           myints,
           delegate (int i) {return (i+1).ToString();});

where I've mapped over an array, producing a list and passing in an anonymous function. 

You can download the free beta of Visual C# Express and compile that code today, within a powerful IDE. Plus C# lets you do all sorts of real-world things (graphics, networking, database access, web page generation,...) considerably more conveniently that most existing implementations of functional languages.

Agreed, the syntax of these examples is a bit heavy (though Java has more inference for type parameters than C#), but if we want to argue the genuine advantages of functional programming, especially in an industrial context, "map" just isn't enough any more.

One possible position is simply to declare victory - generics and anonymous functions wouldn't be in Java and C# if it weren't for functional programmers. Another is to move the discussion to a higher level and start to talk about reasoning principles, rather than comparing lines of code. This is where the real advantages come, but it's a pretty subtle argument to put to a PHB, I fear.

  Nick

-----Original Message-----
From: haskell-bounces at haskell.org [mailto:haskell-bounces at haskell.org] On Behalf Of Vincenzo aka Nick Name
Sent: 15 September 2004 00:25
To: haskell at haskell.org
Subject: Re: [Haskell] Haskell in industry?

On Tuesday 14 September 2004 18:18, Heribert.Schuetz.extern at HVB.de 
wrote:
> *       Can't much of the simplicity of the Haskell code also be
> reached by just switching from C++ to something like Java or C#?
> (Probably an example from the application domain will be most
> convincing. So I probably have to bite the bullet and reimplement
> some code in Java or C#. But if you have some examples, they might be
> helpful.

I don't have examples but I've been working with C# 8 hours per day for 
6 months and I ensure you that no, they are not simple. C++ is simpler 
for enterprise uses than C#, IMHO, because you fundamentally reuse 
other people libraries and so worry _less_ about memory management than 
you'd expect, but YMMV. On the other hand, C# has NO parametric 
polymorphism and after a while I've begun to miss C++ templates. C# has 
functional programming, they say, but no you can't use a method name as 
a function. You first have to create an "event". Composing this with 
the absence of parametric polymorphism and type inference, here's what 
a _monomorphic_ "map" function looks like, with a possibly naive syntax 
because I don't have the compiler handy (and worked 8 hours today,too).

/////
public delegate int myDelegate(int i); //This is a type declaration

public ArrayList map(myDelegate fn,ArrayList list); 
// Note that the list is untyped, but the function is strongly typed.
{
   ArrayList result = new ArrayList();

   foreach (Object obj in list)
   {
      result.Add(fn(obj));
   }

   return result;
}
/////


6 rows of effective code, untyped, contrived and poorly reusable.

And this is the way you call it (you already know, ok, but now I am 
having fun :))

public int increment(int i)
{
  return i+1;
}

map(new myDelegate(increment),myList); // note the call to "new"

The map function from the haskell prelude:

map f xs = [f x | x <- xs]

and how you call it:

map (\ x -> x+1) myList

It's strongly typed, polymorphic and reusable. And it's far more 
readable and clear in intent, too.

Do I need to say more? If you have the possibility to use haskell, ocaml 
or SML at work, do so :) 

OK, I realize that you already knew haskell, but you asked first :)

Vincenzo
_______________________________________________
Haskell mailing list
Haskell at haskell.org
http://www.haskell.org/mailman/listinfo/haskell


More information about the Haskell mailing list