How to make the transition to functional programming?

How to make the transition to functional programming?

tahatmat at Programmers Visit the source

Was this solution helpful to you?

Other answers

Here's an idea: write/rewrite a program in F# as easily as possible, which may be thoroughly imperative make incremental improvements eliminate mutability look for loops that can be replaced with higher-order functions (map, filter, reduce, etc) try replacing trivial type hierarchies with discriminated unions replace conditionals, especially nested ones, with pattern matching try separating classes into "data" (DUs, records) and "behaviors" (modules containing let-bound functions) This is by no means an exhaustive list, merely a "getting started" guide. Beyond this, read good code (F# core/compiler, FSharpx, and WebSharper are all open source) and use F# a lot!

Daniel

The fact that F# allows you to program both in a functional and in an imperative style could make your task a bit harder at the beginning because each time you have some difficulty you'll want to fall back to more familiar constructs and ways of reasoning. Have you considered a jump-into-cold-water approach, e.g. trying to learn a purely functional language like Haskell (a good book is http://book.realworldhaskell.org/) first? This will force you to reason in a functional way for some time and to concentrate on general concepts without being distracted by the imperative paradigm. Like with foreign languages, you learn a language much faster when that language is the only one that you can use. You do not need to become a Haskell expert, I would say that a few months of Haskell is sufficient. When you are proficient enough that you can write a small tool in Haskell (e.g. a tool that can process some text file and extract some information from it), you can go back to learning F# and you will probably find many concepts and a way of reasoning that has already become familiar to you. And especially, you will find it much easier to separate the imperative from the functional aspects of F# as you learn it.

Giorgio

I highly recommend "Real World Functional Programming: With Examples in F# and C#" by Tomas Petricek and John Skeet, it contains /as the title sujests/ a lot of equivalent examples in F# and C# that make it much easier to make the connection. For general introduction to functional programming I also recommend the classic "Structure and interpretation of Computer programs" that is available for free online together with the corresponding video lectures from MIT from 1986 or so... The first 3 chapters did it for me. If you want to go deep take a look at some lambda calculus papers also available online, it is the grand-father of all functional programming languages and is a great joy to explore.

Petar Kabashki

The other answers have good references. My advice is just to restrict your pride for a bit and do it. It will be like learning for the first time again- you'll write a lot of terrible code at the start but you'll get the hang of it. Take a realistic expectation that it will take some time writing some nasty code before you get the hang of it and everything will turn out ok. Best of luck, remember to keep it fun!

anon

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.