I want to learn a functional language that will be good for building web applications in the future. I am choosing between Clojure and Haskell. Which one is a better choice for my purpose?
I don't see either as being a great platform for the web. But Haskell is a lot more mature and has a lot of brainpower behind it. Check out Peter Thiemann's work on WASH and related tools for web programming in Haskell.
As languages in general, Clojure is dynamic and runs over JVM. Haskell has a powerful static type system (one that helps you rather than get in your way) and has both interpreters and native-code compilers. There is a much more mature infrastructure for Haskell, e.g., things like QuickCheck and SmallCheck for really easy unit testing.
If your app is ever going to scale to something large, Haskell is probably the right choice. If you want the dynamic flavor of lisp you'll be happier with clojure. I would say tentatively that learning Haskell will expand your mind more and you'll be able to do more things, and more interesting things, with your new knowledge.
You wouldn't go wrong by learning them both, I imagine. I am learning them both right now. I'm not sure either is particularly what you are looking for as far as web development, but some of the techniques you learn will help you there (especially in writing Javascript, which has first-class functions etc.)
From an objective standpoint, Clojure is easier to learn, in that it is very simple syntactically (being a Lisp). It's not a biggun like Common Lisp, either; it glories in the fact that it is a dynamic, thread-safe supplement to Java. It's also not yet at version 1.0 (though you can for sure write code in it fine now).
Haskell introduces quite a bit more concepts. I think this is a good thing. But it will take more effort on your part.
Not sure about Haskell, but Clojure has Compojure which is a loosely Rails-like web framework. Clojure can take advantage of what Java brings to the table -- mature and stable libraries for many web-oriented things, servlets etc. Jetty is a pretty nice Java HTTP server which is easy to use from Clojure. The JVM is very mature and tested and cross-platform and performs well and is easy to install almost everywhere. Clojure is also designed with multi-threading in mind, which is handy for web applications at times.
One example why a Lisp is good for web development is the large number of sexp-to-HTML libraries, which is by far the best way I've ever found to produce HTML. (One is included with Compojure, and they aren't hard to write yourself from scratch either.)
Then you can write your HTML in Clojure directly as s-exps and let Clojure do the dirty work of closing your tags, indenting, checking for typos etc. You can convert between XHTML or HTML or XML output styles. You can use the full power of Clojure to do looping and "templating" (via function or macro calls) and variable substitution and whatnot. It's HTML templating done RIGHT. This is one of the strengths of Lisp: you can create and use powerful domain-specific languages, and do all of your coding in Lisp directly.
First, I want to add that I'm not sure what people mean by saying that Haskell and Clojure are not suited for web programming. Was Ruby suited for web programming before Rails? Was SmallTalk suited for web programming before Seaside? Of course they were, otherwise people wouldn't have spent the time to build those libraries!
Anyways, it seems to me that Clojure is heavily influenced by Haskell in many aspects of its design (STM, availability of certain functions). Interoperability with Java and it's large library base also give it a huge advantage. Rich Hickey's decision to import good Haskell ideas in to his own Lisp show that there are some features of Lisp that are lacking from Haskell- powerful incremental development environment (via SLIME), modify running programs on the fly, dynamic typing, macros, etc. This importation shows much can be learned from The Haskell Way, but if I were to bet on which language will be more relevant for the web I would bet Clojure.
Being a professional indepedent web programmer by trade, IMHO the only frameworks that really matter at this point (in a forward looking sense) are Rails (and its ilk), Django, and Seaside (and its ilk - Weblocks). We're at the tail end of PHPs dominance for independent web hackers (note, I have no opinions about corporate web programming methodologies). Django and Rails will dominate for the next 4-5 years, but I believe that Seaside style frameworks are the real future of web programming and Clojure seems to be best equipped to support a Seaside style framework.
We could debate dynamic or static typing for ages and never come to a conclusion. We could discuss s-expressions and brackets or requiring correct indentation to structure code, and not come to a conclusion.
My brother and I have this debate over and over. He looks at Clojure and finds it logical, beautiful, loves the dynamic typing, and for the most part agrees with every design decision Rich makes. He actually makes a patch to the underlying language to change the lazy aspect of the language to solve a design methodology decision that he uses, but other than that he sees it as the perfect language.
I look at Haskell and see it as logical, beautiful and I love the static typing.
For him clojure just 'clicks', for me haskell just 'clicks'.
At the end of the day they are very similar languages so using the 80:20 rule which language you pick isn't going to make a huge difference to you.
I think the whole this language or that is a little distracting from the domain. My brother develops mostly web apps, and says that using the jvm and the java libraries is a huge benefit, while I develop server apps and find haskell a nice fit without worrying about the extra overhead of jvm and have access to all the libraries I need.
If you just want to learn a language then pick whichever one feels right for you, but if you specifically want to do web development, then I would suggest Clojure.
I would encourage you to look at the Happstack, a Haskell web framework.
It's plenty fast (can handle over 6000 reqs/second on my modest laptop).
The happstack-state component is very interesting. It is basically a database system which uses native Haskell datatypes, and where your queries are pure Haskell functions. You don't have to try to marshal your data to some limited set of datatypes (such as SQL). Instead you can use expressive algebraic data types, and data structures like tress, graphs, etc. Your query functions are native Haskell code too. So you get fast performance, and a really powerful, complete query language (aka, Haskell). It's also safe from things like string injection attacks.
For additional type safety, you can use things like formlets (for type-safe form processing), or web-routes (for type safe url routing).
Happstack is also designed to be flexible and not force you into only one way of doing things. So, you can still easily talk to SQL databases, etc. And it supports a wide variety of HTML/XML/etc template options.
Using a type-safe language, like Haskell, has been nice for web development work in my experience. Especially when working with a team. When you do a major change in your code, you will get lots of compile time type errors. Once you fix them, things tend to work again. By comparison, if you were to use a dynamic system with SQL, when your database changes, you have no simple way to be sure that all your queries are still going to work with the new schema.
- jeremy