Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

As a result of the comment discussion here, I wonder whether you can learn Functional Programming in C?

share|improve this question

closed as unclear what you're asking by gnat, MichaelT, durron597, Snowman, Ixrec Sep 9 at 19:23

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

Regardles of whether you can or cannot, you shouldn't. – R. Martinho Fernandes Oct 29 '11 at 10:10
Absolutely! The first step is to write a Lisp interpreter ;-) – Ferruccio Oct 29 '11 at 10:54
If I got it right, the question should have been about learning various concepts with pseudocode, without any real language at all. – SK-logic Oct 29 '11 at 11:50
@SK-logic: To put it mildly, very few programmers have learned programming purely with pseudocode, most had to get their hands dirty and their faces smacked with error messages from the compiler/interpreter. – sbi Oct 29 '11 at 11:51
@sbi, some of the best programmers have learned programming when compilers had no error messages at all. Coding on punch cards requires a bit of understanding of what you're doing long before you'll have a chance to run the code. And needless to mention that the basis for the functional programming had been established long before a first computer was built. – SK-logic Oct 29 '11 at 13:27

6 Answers 6

up vote 15 down vote accepted

Obviously you can do functional programming in C. In theory, you can also learn functional programming principles in C, but the language doesn't make it easy.

I assume you have at least a bit of a background in OOP; if you do, you should be aware that OOP can be done in C, including polymorphism, getters/setters, visibility rules, etc. etc., but it's fairly painful to do so, and you need to know both OOP and C inside-out to pull it off. It's much the same with FP.

What you should be doing is first learn a functional programming language (most of them have surprisingly simple syntax rules; it's not the syntax that makes them hard to learn), and then let your newly-acquired wisdom influence the way you write C.

As per request, a few things you can learn from FP and then apply in, say, C, C++ or Java:

  • Avoid state, especially shared mutable state
  • Appreciate pure functions
  • Appreciate lazy evaluation
  • Being able to model in terms of verbs rather than nouns
  • Being able to approach problems recursively as well as iteratively
  • Using higher-order functions
  • Thinking of functions as just another kind of value, that you can pass around and combine with other values
  • Using first-class functions as an alternative for object-based polymorphism
share|improve this answer
Can you provide specific examples of how C / C++ / Java programmers do can benefit from LISP wisdom. It makes sense in theory, but I am looking for something concrete. – Job Oct 29 '11 at 14:51
@Job, What concrete benefit can there be? It expands their minds and maybe makes them think of excessive mutable state as a bad thing, what more could you ask for? – dan_waterworth Oct 29 '11 at 17:51
So can I conclude from this that, while it is possible to learn (some) FP in C, it makes no sense to do that? – sbi Oct 29 '11 at 17:54
@Job: I have learned FP when I learned LISP as a student many years ago. About a decade later I applied FP in template meta-programming. – sbi Oct 29 '11 at 17:55
@sbi, my favourite way of learning new languages and concepts is via implementing them. And C is quite a decent language for implementing a compiler. So, yes, you can learn FP with C. – SK-logic Oct 30 '11 at 11:39

I think that the tools you use influence your learning a lot. It's almost impossible to learn programming concepts for which the programming language you use does not provide the means to make use of. Sure, you can always learn a few things, but you cannot learn it properly.

But that is academic anyway, because, as Martinho says in his comment, even if you could learn functional programming, you should not try to do that, because there are languages where this is much easier.

share|improve this answer

You shouldn't learn functional programming in C, but in a strict functional language (Haskell, Caml, Erlang, etc . . .).

If you are new to functional, you will never really get it with a non functional language. More likely, you will train yourself to do what you think is functional programming and learn things the wrong way. And it is always harder to « relearn » things the right way than learnt them the right way at first.

Anyway, I think doing functional in C is a good exercise for someone who already knows functional. Because that person will learn what's going on behind the hood - what the computer is really doing.

share|improve this answer

C can be hacked to offer some functional concepts:

This StackOverflow question will tell you more. But although it seems possible to do functional programming (or a large subset of) in C, hacks and compiler extensions and whatever are not the best way to learn of a concept.

To actually learn functional programming your best bet is one of the prominent functional programming languages like Lisp and its dialects (Clojure, Scheme), Erlang, and Haskell. Any one of those are perfect tools that work within the functional programming mindset. F# is also a good candidate if you have a .Net background, but it's a multi paradigm language, not strictly a functional programming language.

As tdammers notes in the comments:

Actually, LISP, clojure and scheme are also multi-paradigm; Haskell, while being pure and default-lazy, also allows for imperative programming while in a monadic context, and it has extensive support for concurrent processing. All of these have mechanisms that implement large parts of the wisdom gathered in the OOP world - encapsulation, inheritance, single-responsibility, composition, etc. It's not so much about whether a language ALLOWS other paradigms; it's about which paradigm forms a language's starting point.

To the best of my knowledge Lisp and its dialects and Erlang are better candidates than F# because they encourage functional programming over other paradigms, what tdammers beautifully states as a language's starting point. F# does encompass functional programming but does not encourage it over its other supported paradigms, imperative and oo programming.

share|improve this answer
Regarding your last sentence: In that case I'd argue that F# is a better candidate because it won't trap you in a functional mindset. In a multi-paradigm language, when you start hammering screws, you can just switch to a screwdriver. When trapped in a single paradigm you may miss the difference between the screw and the nail. – R. Martinho Fernandes Oct 29 '11 at 11:05
Well the question is on how to learn functional programming, not programming. I'm assuming that the op has some experience in a multi paradigm language and wants the most efficient way to learn functional programming, in which case F# is a good candidate but not a perfect one. Trapped in a single paradigm is obviously the best way when you are looking to learn of that single paradigm as you don't have to deal with everything that's not it. – Yannis Oct 29 '11 at 11:10
I believe that learning when the paradigm is well-suited and when it is ill-suited for a certain task is a part of learning it, perhaps the most important. – R. Martinho Fernandes Oct 29 '11 at 11:14
Actually, LISP, clojure and scheme are also multi-paradigm; Haskell, while being pure and default-lazy, also allows for imperative programming while in a monadic context, and it has extensive support for concurrent processing. All of these have mechanisms that implement large parts of the wisdom gathered in the OOP world - encapsulation, inheritance, single-responsibility, composition, etc. It's not so much about whether a language ALLOWS other paradigms; it's about which paradigm forms a language's starting point. – tdammers Oct 29 '11 at 11:19
@MartinhoFernandes: One could argue that being trapped in a single paradigm is the perfect way to learn when it is really a pain in the ass :-) – Jörg W Mittag Oct 29 '11 at 12:45

You cannot learn all aspects of functional programming in C. But surely you can start functional style programming with any imperative language. These starting bits are- "How to keep things pure while programming." And it can be done C also. Check this blog post for details-

share|improve this answer


Functional programming is about closures and their applications. Unless someone is able to show you a descent closure library for C, forget about using C to learn functional programming.

What is functional programming?

The cardinal concept of functional programming is the notion of closures which roughly speaking, captures a function together with variables bindings. Besides the pervasive use of closures, there a few other distinctive traits in functional programming, like the use of recursive functions and immutable values (both play well together). These traits are more a cultural issue than anything else, and there is no technical obstruction to use them in virtually any language, this is why I focus on closures in my answer: not every language allow to easily create closures.

Three illustrations of closures usefulness

A typical use of closures is the implementation of privacy mechanisms. For instance the Javascript code – in the examples I picked Javascript because it is a functional language with a so-called “C-like syntax” and your question suggests that you are familiar with C:

create_counter = function()
  var x = 0;
  var counter = function()
    return x;
  return counter;

Then with

a = create_counter();
b = create_counter();

we have two functions a and b counting disjoint collections. The point of the example is that the variables x is captured by the closure defining the counter closure and each time a new counter closureis instantiated by the function, it gets its fresh own idea of whatx` is.

Another typical use of closures is the definition of partial applications of functions. Assume that that we have a reporting facility similar to syslog implementing a function

var log = function(priority, message) {

where the arguments priority and message are expected to be strings, the first one being one of "debug", "info", and so on. We can define a log factory like this:

var logWithPriority = function(priority) {
  return function(message) {
    log(priority, message);

and use it to define specialised versions of our log facility:

var debug = logWithPriority("debug");
var info = logWithPriority("info");

This is very useful, because instead of writing error-prone for-loops like this

for(i = 0; i < journal.length; ++i) {
   log("info", journal[i]);

we can write the cleaner, shorter and much simpler (there is no i, that's much better):


A third important application field of closures is the implementation of lazy evaluation – note that special language support can provide for a better implementation.

A lazy function, instead of performing a straight computation, returns a closure which can be called (or “forced” in the jargon of laziness) to perform the question. The motivation for doing this is that it separates preparing a computation and performing a computation. A practical example of this is regular expression compilation: if a program compiles a lot of regular expressions at startup-time, it will need a lot of time to start. If instead we lazily compile the regular expressions and force them as we need them, then our program can quickly start. Of course, regular-expressions can be substituted here with any structure requiring considerable initialisation time.

Here is how to implement lazy evaluation with closures. Consider the classical implementation of the arrayMax function returning the max in an array:

function arrayMax(array) {
  return array.reduce(function(a, b) {
    return Math.min(a, b);

The lazy variant would be:

function arrayMax(array) {
  var memo = null;
  function actuallyCompute() {
    if(memo === null) {
      memo = array.reduce(function(a, b) {
        return Math.min(a, b);
    return memo;
  return actuallyCompute;

The returned value is a closure which can be used to compute the value or retrieve it another time if it has been already computed.

With these three examples, we should be confident that closures and their applications are the core of functional programming.


Learning functional programming means learning how to program with closures. As a consequence, languages allowing the easy manipulation of closures, and especially the partial application of functions, should be regarded when looking for a language to study functional programming. Conversely, languages where closures cannot be easily manipulated would be poor choices.

share|improve this answer
Thanks for this nice sample. BTW, as you define var info, wouldn't it be journal.forEach(info) ? – ejaenv Sep 3 at 17:45
Yes, it would be an appropriate variant! :) – Michael Grünewald Sep 8 at 20:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.