7

I have 6 years of programming experience, mostly following the object oriented paradigm, and I'm interested in learning functional programming. My main goal is to become a functional paradigm programmer and not a programmer who is using a functional language but still writes in imperative style.

When I'm practising in functional programming I still follow an object oriented way of thinking, like loops, mutable variables and so on. I picked F# as a basic functional language and because it's very tightly integrated with other .Net languages, like C#. F# allows usual object oriented statements, therefore you can use F# syntax but not actually produce functional code.

What statements and approaches should I avoid when learning functional programming?

4
  • 11
    You should avoid statements. Aug 12, 2012 at 5:42
  • I'm afraid this question might be a bit too open-ended for this site, but in any case, I would recommend reading this post by the ever-brilliant John Carmack: altdevblogaday.com/2012/04/26/functional-programming-in-c
    – TehShrike
    Aug 12, 2012 at 7:07
  • Think in terms of expressions, not statements.
    – SK-logic
    Aug 12, 2012 at 10:10
  • 2
    If you really want to learn to think functionally, take a purely functional programming language like Haskell. When you have become fluent in Haskell, you can consider hybrid languages like F#, Scala, and so on.
    – Giorgio
    Aug 12, 2012 at 18:44

3 Answers 3

9

Functional Programming is more than just a list of statements to avoid. It's a (often) radical shift in mindset. For example, there are no destructive state changes allowed in functional programming. If I say that A = 5, I have not set up A as a kind of box containing a 5. I've made A exactly the same as 5, forever. There's a very very good book, co-written by the one and only Jon Skeet called Real World Functional Programming.

As far as learning purely functional programming is concerned though, I'd actually recommend Haskell over F#. Granted you'll probably get more done in terms of production code with F# or Ocaml but Haskell will bend your mind into the proper shape in order to grok functional programming. The process may take some time, however.

I recommend Learn You a Haskell if you want a guide to that. The C#/F# book option is less painful though. C# and F# let you solid, workable magic as a functional programmer. Haskell is black sorcery by comparison.

5
  • 1
    Well, you didn't make A exactly the same as 5 forever. You merely bound it to 5 for this specific context, and can't re-bind it. You can have plenty of other As elsewhere, and of course also bind something entirely else to A the next time you come by whereever you bound A to 5 (say, during recursion or an independent call).
    – user7043
    Aug 12, 2012 at 3:09
  • @delnan Right but I'm being overly dramatic for emphasis ;)
    – user28988
    Aug 12, 2012 at 3:09
  • I don't doubt you know that, but some readers may not - and I wouldn't want yet another guy confusing state with mutability ;)
    – user7043
    Aug 12, 2012 at 3:12
  • 1
    I got stuck with Learn You a Haskell - some of the examples don't work in the current GHC.
    – Oded
    Aug 12, 2012 at 8:06
  • Also, at least in F#, you can certainly re-bind A to 6. That's called "hiding", or something similar. You're just hiding the old value called A with a new value called A. Aug 13, 2012 at 16:02
4

Learning functional programming is not about applying what you know from imperative and object-oriented programming and leveraging that to learn functional programming. In your question you are asking what to avoid, when I believe you should be thinking is: To learn functional programming I realize I should totally set aside what I know about imperative programming; where should I start.

As some starting advice see How to make the transition to functional programming?

So don't convert imperative code to functional code. If an algorithm is amiable to functional programming then use functional programming. If an algorithm is amiable to imperative programming then use imperative programming.

To answer your question more specifically

In the object-oriented world objects are king, in the functional world functions are king.

2

Probably the most important thing to avoid is using constructs that change state.

Many people who start learning functional programming try to "emulate" imperative thinking in a functional language. This results in poor programs that make them feel that functional languages are cumbersome and ineffective. So it's important to get rid of imperative thinking. Don't think about the steps the program should do, think about expressions (functions), what they mean, and how to combine them to get the desired result.

Also, don't be too concerned about the effectiveness of your program at the beginning. Imperative programmers often feel that creating new instances of data structures each time they want to "change" something must make the program slow and ineffective. Functional languages are well optimized for that.

As stated in another answer, F#, OCaml, Scala and many other functional languages allow state-changing constructs, side effects and imperative sequences of statements. On the other hand, Haskell doesn't permit that and so you have no choice but to learn how to do it in a purely functional way. (I've seen many people describing it as "rewiring one's brain".)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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