As a result of the comment discussion here, I wonder whether you can learn Functional Programming in C?
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:
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.
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.
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:
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.
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-
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.
Two illustrations of closures usefulness
we have two functions
Another typical use of closures is the definition of partial applications of functions. Assume that that we have a reporting facility similar to
where the arguments
and use it to define specialised versions of our log facility:
This is very useful, because instead of writing error-prone
we can write the cleaner, shorter and much simpler (there is no
With these two 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.