Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

share|improve this question
21  
Regardles of whether you can or cannot, you shouldn't. –  R. Martinho Fernandes Oct 29 '11 at 10:10
20  
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
3  
@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

5 Answers 5

up vote 0 down vote accepted

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 Rizos 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
2  
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
1  
@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

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
1  
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
    
@Job: I can think of quite a few examples, yes. I'll update my answer in a second. –  tdammers Oct 29 '11 at 21:55

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-

http://www.johndcook.com/blog/2011/07/24/get-started-functional-programming/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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