Is functional programming relevant to web development?

Viewed 25396

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'?

19 Answers

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.

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)

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.

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.

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.

Yes, as functional programming can be done in any language, you can use it as a web developeron a day to day basis.

Should you? it depends upon the problem you are solving. Functional programming is a programming paradigm and where you should use it depends upon the problem you are solving.

To make the decision simpler, think whether it is easier to solve some problem by using OOPs concepts, where encapsulation, polymorphism, inheritance like features can make your life easier?

If yes, do not go for functional programming there and simply use OOPs. If your application is going to perform complex computation / calculation / business logic and involving heavily concurrent processing, Functional programming can offer lots of tools and advantages in such cases.

These are just different styles of building the structure and elements of programs, so it’s all about using right tool for the right job.rest anything can be done using anything.

Functional programming in web development:

JavaScript supports functional programming and it is very much supportive when we are in context of web development.React framework is heavily influenced by functional programming principles and used in many web applications out there.

Also, you can find many web applications built and running with frameworks developed on functional programming languages listed below:

• WebSharper (F#)

• Snap (Haskell)

• Lift (Scala)

• Ocsigen (OCaml)

• Chicago Boss, Zotonic (erLang)

Hope So my answer will help anyone.

Related