A couple of months ago, we posted about the research program of SocioPLT, whose goal is to study why some languages get adopted more than others. As part of a study, the investigators ranked languages according to their frequency of use in Sourceforge projects during the period 2000-2010. An interesting finding was that among the top 20 languages in these rankings, there wasn’t even one functional language.

Academic PL researchers tend to love functional programming, so many of us would find this fact depressing. However, it seems that we academics are at least partly to blame for this state of affairs. For functional programming to be embraced in the “real world”, universities must teach it. In this post, I show that few universities do. In particular, functional programming is a significant part of the core programming sequence at only 9 of the top 33 American schools, and is not offered at all in about 40% of these schools.

Why functional programming?

There are good reasons why PL researchers like functional programming. In imperative languages like C, a line of code within a procedure can affect the entire global state of the program. In contrast, in functional languages, such side effects are severely limited. This makes it much easier to reason about correctness and security: analyzing the correctness of the composition of procedures A and B can be broken down into analyzing A and analyzing B. Researchers have also frequently argued that functional program design makes it fundamentally easier to exploit parallelism.

Why is functional programming unpopular?

So why is functional programming unpopular in the “real world”? Consider some non-reasons first.

The argument “functional programs are inefficient” isn’t really sensible any more (if it was ever). First, because of advances in compiler technology, implementations of functional languages are now more efficient than ever. As just one data point, in this comparison of two ray tracers written in C++ and the functional language Ocaml, the Ocaml implementation performed comparably with the C++ version, while having 47% fewer lines of code. Second, a survey of programmers by the SocioPLT researchers showed that a language’s performance was not among the top five factors when choosing it for a project.

The same survey also showed that the factor that correlates the most with preferring and using a language is the availability of libraries. This is certainly behind the meteoric rise of, say, Python. However, it seems implausible that this factor is the primary reason why functional programming is unpopular. If it were so, the F# language, which allows functional programmers to utilize the entire .NET infrastructure, would be a mainstream language by now. There’s no indication it will be any time soon.

The real problem: Education

By far the biggest issue with functional programming, it seems to me, is that most programmers aren’t familiar with this programming style. This is corroborated by a second survey from the SocioPLT folks, which reports that only 22% of the surveyed population were familiar with a functional language. Among participants who had “learned” a functional language in school, those claiming familiarity went up to 40% (an improvement, but still not impressive). In contrast, 94% of the participants knew at least one of C/C++, Java or C#, and 79% knew at least one of Perl, Python or Ruby.

The unavoidable takeaway from this is that university CS departments aren’t doing a good job exposing their students to functional programming. Even when they teach functional programming, the exposure is superficial enough to fade over time.

You ought to see this as a problem even if you aren’t a fan of particular functional languages. By programming in a functional language, a student encounters some fundamental insights: that data can be defined inductively, that many interesting applications essentially involve pattern-matching on data-types, that code is not fundamentally different from data, that keeping side-effects minimal makes composition easy, … These insights are useful even if you are going to program in Java or C++.

Who teaches functional programming?

So perhaps it’s worthwhile to investigate which schools do and don’t offer functional programming. Over at the CACM blog, Philip Guo recently posted about introductory languages used at the top 39 American universities (as defined by US News rankings). Only five of these schools used the functional language Scheme in their CS0 or CS1 course; “zero or one” used a statically typed functional language like ML or Haskell. Several of these schools have moved away from using Scheme to using Python. It’s clear from this data that few students learn functional programming in their first year in college.

But how many schools give their undergrads a serious exposure to functional languages, at any level? I couldn’t find this data anywhere, so I decided to do some quick digging. Below, I tabulate American universities ranked 1-30 in the US News 2014 computer science rankings, along with information about the first real functional programming course that they offer. For a course to be counted, it has to satisfy the following criteria:

The course must require a substantial amount of functional programming. I define this as “30% or more of the assignments must involve functional programming.”

The course has to be offered regularly. It must have been offered in 2013 or 2014.

The class has to be reasonably general-interest. For example, Yale has a class on computer-aided music where students (apparently) do a good amount of Haskell programming. I wouldn’t count this course in my table. On the other hand, classes on compilers or programming language implementation that use functional languages are counted.

Classes that are required, or appear to be taken by a large number of students, appear in bold.

Conclusions

As we can see, several schools teach functional programming as part of their core programming sequence. For some of these schools (e.g., Minnesota), this appears to be a recent development. Of course, over the last few years, some schools (notably MIT, UC Berkeley and my home institution Rice) have also moved away from using functional languages in their core curriculum. I am not sure if the total number of schools that teach functional programming as part of their core programming sequence has gone up or down in recent times.

At some other schools, functional programming is taught in PL classes. This seems suboptimal, because I imagine that in most cases, these courses aren’t required. This may also tie functional programming too closely to the particular application of interpreter and/or compiler design. Of course, this is still better than not covering functional programming at all. Unfortunately, many of the schools, including high-profile schools like MIT and Michigan, don’t offer their students any exposure to functional programming.

How do we change this? Of course, university curricula are zero-sum games; in many universities, there just won’t be a spot for a new required programming class. One possibility, then, is to embed functional programming into lower-level required courses. I personally think that a course on data structures and basic algorithms could be a great vehicle for functional programming. Some schools already have a course of this sort. For instance, CMU’s data structures and algorithms course (15-210) was recently revamped to use only functional programming, with particular emphasis on persistent data structures and the exploitation of parallelism. Cornell’s CS 3110 is another excellent example of such a course.

It could also be nice to have more classes of the form “Functional programming for X”, where X is something other than interpreters/compilers and foundations of PL. In my research, I didn’t find too many classes like this. One exception is Purdue’s course CS 390, which is on parallelism and concurrency but uses functional languages for about 30% of the course. Another is Stanford’s course CS 240h, which teaches how to build system software using Haskell. But clearly, much more can be done along these lines. As examples, I’d love to see a course on data science using F#, or building secure-by-construction systems using Haskell!

[Caveat: I am by no means guaranteeing that the data I gathered is correct. If you find anything wrong with it, point it out in the comments, and the bug will be fixed.]