views:

4851

answers:

18

I've been seeing so much recently about functional programming and Clojure looks particularly interesting. While I 'understand' the basic description of what it is, I can't figure out how I would use it on a day to day basis as a web developer, if I can at all. A lot of what I have read focuses on the maths side of functional programming rather then typical programming situations found in regular OO.

Have I got the wrong end of the stick? Is functional programming totally un-related to web development? If not, are there any examples of it being used 'for the web'?

+12  A: 

A few examples off the top of my head:

  • Yahoo! Store is powered by Lisp (originally named Viaweb prior to acquisition)
  • Reddit was fully prototyped in Lisp, although they switched to Python in 2005
  • Hacker News is written entirely in Arc (a Lisp dialect)
Greg Case
Wow, Hacker News is _fast_.
Svante
@Harlequin - Yes, but I suspect that has more to do with the simple page layout than with the implementation language.
Sherm Pendley
Yahoo! Store was rewritten in c++ a few years back.
Andrew Gwozdziewycz
language != programming paradigm.
phkahler
Odd choice of examples... Yahoo! Store was written by Paul Graham, who is a big Lisp advocate. He also wrote Hacker News, which is, effectively, a Reddit clone. Presumably, HN was written somewhat in response to Reddit's switch from Lisp to Python while the Reddit guys were part of YCombinator: an accelerator program that is run by Paul Graham. I'd count this as ONE example, not three.
Brandon Bloom
+1  A: 

It is not totally unrelated to web development. The app sitting on the server can very well take advantage of functional features like closures, higher-order functions, immutability, referential transparency... for instance, you sure have collections that you need to transform or manipulate in whatever way. Functional programming helps here, and it is for a reason that its idioms are penetrating mainstream languages. Functional features help in conciseness, testability, parallelization, and they can also provide native solutions to problems you would otherwise solve with patterns.

Update: there are web frameworks for functional languages too. Weblocks for Common Lisp, Lift for Scala. These are the ones I've heard of, there might be more... however you don't necessarily have to be purely functional -- for example Scala is not pure and should work with any Java framework, you'd still be able to use functional programming for the business layer, etc.

Germán
+7  A: 

I don't see why not - so long as you're delivering standards-compliant HTML to browsers, they don't care what you used to produce it, be that a functional language, an imperative language, or trained monkeys.

Sherm Pendley
Nice that you mention monkeys :)
Sergey
+26  A: 

Functional programming matches web apps very well. The web app recieves a HTTP request and produces a HTML result. This could be considered a function from requests to pages.

Compare with desktop apps, where we typically have a long running process, a stateful UI and dataflow in several directions. This is more suited to OO which is concerned about objects with state and message passing.

JacquesB
+1 web apps should be stateless, and FP apps shouldn't have to worry about state.
Matt Ellen
+1  A: 

They are doing some pretty cool things at Edinburgh University with functional programming for the web.

Ali A
+8  A: 

Pure functional programming might not map very well into the web programming environment. But the main impediment is just the lack of infrastructure (frameworks and APIs). It will be a long time (probably never, honestly) before a functional language has as rich a web programming environment as Java, Python, or Ruby.

That said, there are some options.

I don't have any experience with any of these. Maybe commenters can weigh in on what's worked well for them.

Chris Conway
don't forget yesod http://docs.yesodweb.com/
barkmadley
+9  A: 

Twitter rewrote their backend in Scala, a JVM language that supports both the Object Oriented and Functional paradigms.

Also, the Lift web framework is written in Scala.

James McMahon
A: 

Here is a web developer experience in building web apps using Haskell. Although functional languages are very type safe and have good concurrency, they always lacked best of the breed api's since it has been darling of the academia since long and yet to be strongly embraced in the real world. I hope it's not too far away. Erlang had already got into it.

A_Var
+1  A: 

Language and paradigm doesn't matter for web apps, they're all equally good and bad. If you are looking for a reason to learn a new paradigm, just dive in. My advice would be to analyze the problem you're trying to solve and select the appropriate toolset.

Chuck McKnight
A: 

The reason why functional programming is popular with web programming is that it explicits shared and changing state and allows the programmer to express the purely functional parts as pure functions. Pure functions have the advantage of being very simple to run in parallel - as they have no side-effects.

At least that's my reason.

+1  A: 

You probably won't use it and shouldn't use it but when you say that someone will always find an exception to the rule (Viaweb, etc.). Basically there is no "super language" there are only working lines of code, usually in "Blub." Even Paul Graham says that the main (in fact only) benefit to a Lisp is one's ability to rapidly prototype.

Also "super languages" usually impinge rather than increase code readability, meaning that the one "genius" who wrote it has to maintain it forever since no one else can understand it, especially since he is likely to write it in his own modified dialect. This decreases the possible scope of any project, which means that even if new, innovative things can be done, they are not extensible and so remain at a relatively small scale (like Hacker News in Arc).

That's not to say that someone can't have a genius idea and implement it in a incomprehensible style that then can be re-written in Blub and extended so that lots of people can benefit from it. Actually, that's exactly what has happened in all of the Lisp success stories, not to mention every famous philosopher that's ever lived. But of course, if you are a "genius" you might also be able to prototype your product some other way.

As for FP on the JVM, there are limited but cool things possible. Although I would personally use it for prototyping only, it is possible you might have a use case (usually something to do with multi-threading) where it provides some improvement.

Joel
Actually, Paul Graham says that Lisps big advantage is that it's "the most powerful" language available. He supports his argument by saying that something like a quarter of viaweb (by lines) was Lisp macros. Meta-programming in most languages (particularly those available in the mid-90s) is hard. It's not a prototype if it's pushed to the production server.
Nathon
A: 

Check out Ur/Web. It's very fast and its static type system knows about things like HTML and SQL so it can guarantee all sorts of nice things about security.

Cyrus Omar
+1  A: 

Erlang seems to be getting a lot of use in some of the infrastructure for scalable web apps. the CouchDB and Riak databases are written mostly in Erlang, as is the RabbitMQ message queueing server. One of the keys to its success is that it handles concurrency through message-passing, with no shared mutable state. It's this way of thinking about problems that is useful, more than any particular functional programming language.

Or, look at MapReduce. It's a very functional way of looking at computation, even if your map and reduce functions happen to be internally stateful -- and for that reason, it's a very nice fit to querying large datasets in a fault-tolerant, distributed way.

By all means, go with whatever seems most practical for you. But always keep functional programming in the back of your mind, because you never know when it might come in handy.

Peter Scott
+2  A: 

Javascript (the language of the FE portion of the web and increasingly the BE as well) isn't functional per se but functions are first order functional

A: 

Hi, For Clojure, there's an interesting startup (TheDeadline) that developed using Clojure and Google App Engine. They have a good ppt on Slideshare and an interview on InfoQ.

For a good discussion on deploying Clojure with GAE: http://news.ycombinator.com/item?id=1239788

As far as I know, Clojure has a few web development libraries. Compojure Ring Conjure

Hope this answers some of your questions =) (I'm just starting out too..)

Best, Ryan

Ryan Teo
A: 

Another short answer: http://www.mlstate.com -- a full web development platform, based on FP. The clean semantics of the language permit all sorts of automated safety and security analysis, optimizations, etc.

Caveat: I work there.

Yoric
A: 

We have just launched an online spreadsheet where the backend is written entirely in Erlang.

http://hypernumbers.com

By any standards this is one of the most complex web-apps you can build with a ginormous GUI with mental complexity.

Gordon Guthrie