views:

261

answers:

4

I have been working with c# for many years, since beta. I am having a bit of a time of it, moving from OOP to FOP. I started out with the concepts of procedural programming and functional composition in the late 80's and have been doing OO since around '95 when UML was in its infancy where I was programming in Delphi with the multi-paradigm approach of Procedural and OO.

Are there any good books suggestions, to helping the transition over to functional Programming from a deep rooted OO programmer. I tried a Haskell book, but it seemed to require some background in the subject and was very unapproachable.

I have a couple of O'reilly, Apress books on F#, but it all seems a bit woolly and half way house, of just getting the job done. It also mixed Objects up with functions to suit the needs.

F# seems to forgive me in my OOP thinking a lot more than Haskell, but I think to get the true benefits I need to get into the mindset of FOP to get the best out of the code. I have read around functional programming for about 2 years in between my day to day c# stuff. But I feel I am not getting into the thinking.

I am pretty sure that there are many people out there like myself. Any answers would be I think a great help to a lot of C# guys wishing to move over to F#. Especially with many big hitters in the market place asking for F#. I am a contractor who needs to get up to speed as soon as possible.

thank you in advanced.

+8  A: 

This is definitely subjective, but I think that many people are asking similar questions. I certainly did when I started learning F# (as someone with C# experience). Here are some assorted ideas:

  • Pick the right learning problems - The best way to learn functional programming is to start working on some non-trivial projects. If you pick a project that is easy to solve in the OO way, then you'll probably lean towards OO solution. However, if you choose some project that naturally fits FP solutions, then you'll learn something. In my case, I worked on translator from F# quotations to JavaScript, which involves a lot of recursive processing and discriminated unions.

  • Don't try to be perfect - If you try to write something in the functional way, it may not be perfect at the first attempt. Don't worry about that (and don't say to yourself that you could do better in OO way). You can do better in the FP way as well - it just takes some time to find out how to do that.

  • Try to be purist - If you start learning Haskell then you'll have to write pure functional code. F# doesn't enforce that, but I don't think that makes F# less suitable for learning functional programming. If you know C# and .NET, you can reuse a lot of your experience. Just try to be more strict to yourself and avoid using non-functional constructs like mutable state and inheritance - they are sometimes useful, but you can use them after you learn to think in the FP way.

And a little shameless plug - answering a question like this was one of the key motivations for my Real World Functional Programming book, so maybe that could help...

Tomas Petricek
Your right on the money with the bullet points. All three relate to my experience to the letter. :)
WeNeedAnswers
I found that with F# it's often hard to know whether or not you're being pure (teehee!) enough, which led me to spend excess time trying to figure out whether or not I'm doing something right. Later I learned Haskell and wish I'd started with that first. Then again, one person on SO who taught Haskell said he found it to be a difficult language for beginners, so I'm pretty torn on whether I should recommend Haskell or F# to beginners coming from imperative .NET.
Rei Miyasaka
I definitely recommend Tomas's book.
Benjol
Not an objection to being a purist as such, but I remember learning C++ and OO coming from a C background, and the advice at the time was basically to forget everything about procedural programming and embrace the new age, what in the real world wasn't possible, but we all muddled through anyway. :) In the end all the new constructs become clearer when you have actual use cases, I guess. Much more intriguing the OP's assertion that "many big hitters in the market place [are] asking for F#". Can we have any names?
Alexander Rautenberg
@Alex, European investment banks based in London :)
WeNeedAnswers
@WeNeedAnswers Names, addresses, phone numbers?
Alexander Rautenberg
@Alex: I'm not really recomending to be a purist when _using_ F# in practice once you learn it. However, it is a good thing while learning F# as it forces you to learn the new things (that you don't know from OO) well.
Tomas Petricek
@Alex, I saw a couple of jobs out there, one was UBS the other was deutsche bank, both were working with derivatives. I liked the look of them, but ended up saying no, heck of a journey from South Coast to London. Was paying around £60k to £100K. enough for me to move out of contracting :)
WeNeedAnswers
+2  A: 

Are there any good books suggestions, to helping the transition over to functional Programming from a deep rooted OO programmer

The Structure and Interpretation of Computer Programs (SICP):

His course seems to focus mostly on functional composition. It is in LISP, but it is less about the language, and more about the abstract concepts.

I don't think he says much (or anything) about pure FP, but I haven't really read all of it, either :) The concepts and way of thinking should still be highly applicable, though.

Merlyn Morgan-Graham
thanks for the link. Will take a look at it.
WeNeedAnswers
Please explain your downvotes guys...
Merlyn Morgan-Graham
+2  A: 

I believe that to really understand functional programming you must first reorganize all your knowledge about the ways to implement different things (e.g. you have to understand that objects may be represented as hash maps or even associative lists, not just as records). Also you will need to start thinking in terms of immutable data and pure functions (procedures of no side effect). Then you'll have to compare functional and procedural ways to implement and treat objects.

All of this you can find in SICP - classical book for learning not only FP, but programming in general. To understand functional way you won't have to read all book, but at least first 3 chapters.

After you understand main FP concepts and are able to implement anything in it's style, read any book on statically typed languages. E.g. Apress books on F# will be much easier to read.

Andrei
I tried the Apress books, the Don Symes ones are great. I would like to get a handle on functional programming though, language agnostic at first, but I will be doing F#. I just wish there was a good "Learn haskell for idiots" book out there. The one I picked up hurt my brain. It was very dry, I picked it up on a University Campus a couple of years back, all was going well, then I hit the worst introduction to Monads I have ever seen and I lost faith in the book.
WeNeedAnswers
@WeNeedAnswers: if you really want something language agnostic, maybe check lambda calculus. I have studied it during my college days and that somewhat helped me when learning F#.
Stringer Bell
@Andrei, why would you want to represent something as an object in a Functional Language? This would imply mutable state.
WeNeedAnswers
@WeNeedAnswers: Objects are not only class instances - it is just any... any object in your program - parameter of a function, list, map, function, etc. In my concrete example "objects" mean key-value pairs, same as property-value in OOP languages. And it doesn't apply mutable state: even if you have to change one "property" of an object in FP, you must recreate this object with needed field replaced - old object stays the same.
Andrei
+1  A: 

Learn You a Haskell is a decent read, though it's probably a bit too basic given you probably already know the syntax of Haskell from one of those books that you picked up...

Moving up a notch, there's Programming in Haskell, which if you scroll down on that page, has a bunch of links to video lectures based on the book given by MS Research's Erik Meijer.

The Equational Reasoning stuff is particularly interesting, if only academically.

In terms of application, that Real-World Functional Programming book given in the other answer looks really nice -- though I haven't read it, so I can't make any judgments.

Rei Miyasaka
I love the Learn you a Haskell link. Its got pretty colours and everything. Shame that it is too basic though, would have been great as a first look book. Much better than http://www.amazon.co.uk/Haskell-Functional-Programming-International-Computer/dp/0201342758
WeNeedAnswers
The reasoning bit is the hard stuff. getting my head around Combinator Logic as a means of decomposition. I am getting it, just that its all a bit uncomfortable. I just want to new up some Classes, which is damn easy in F# but I feel that this is wrong from what I understand this far in FOP. The state stuff is also strange, so used to using Encapsulation.
WeNeedAnswers