As a result of the comment discussion here, I wonder whether you can learn Functional Programming in C?
closed as unclear what you're asking by gnat, MichaelT, durron597, Snowman, Ixrec Sep 9 '15 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.
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:
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
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
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:
The lazy variant would be:
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.
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.
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 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-
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.