views:

532

answers:

7

I would like to hear from you folks that have achieved a high-level of proficiency in F# (and also in functional programming in general too) what should be my steps from now on to become a better/professional F# programmer?

I already know much of the F# syntax and have some years of experience with C++. My goal is, as an engineer and mathematician, to design better scientific libraries (linear algebra packages, partial differential solvers, etc.).

+6  A: 

Like any other language now that you know the syntax and the basics it's time to write code and more code.

  • Make sure you have the core concepts of functional programming down.
  • Work on a large project so you also get familiar with the large and not just the small.
  • Write a few immutable data structures.
  • Work on a large project without using inheritance.
  • If your familiar with design patterns implement some of them in a purely functional way and notice how some of them disappear.
  • F# is about mixing functional and OOP styles. Once you've done a fare amount of abstraction without inheritance bring it back and start mixing the styles together. Find a balance.

Since your goal as an engineer and mathematician is to design better scientific libraries may I suggest as a learning exercise working on a video game style simulation. Something that involves physics and math but also requires control of state.

gradbot
This was what I did when I started learning C++ (~4 years ago). I implemented a "Asteroids" game with spaceships, bullets, etc.. I think this might help now with F#. Thanks a lot gradbot.
Allan
+19  A: 

what should be my steps from now on to become a better/professional F# programmer?

Keep coding everyday :)

I jumped on the F# train in Sept '07, before that I had a boatload of C# experience. It took about 3 months or so for me to stop writing code as C# with a little funnier syntax and start picking up on the right coding style :)

Tips and things that helped me:

  • I found that F# makes it really hard to write non-idiomatic code, really easy to write good, clean code. If you find yourself fighting the compiler, 9 times out of 10 your doing something wrong. Go back to the drawing board and try again.

  • The entire concept of immutability was a mystery at first, but implementing all of the data structures from Okasaki's Purely Functional Data Structures was hugely helpful.

  • During some slow days at work, between '08 and '09, I wrote a wikibook. I haven't looked at it in a while, but I'm sure its really bad --- but, the experience of explaining the language to others was a good jumpstart for someone like me who normally wouldn't have enough motivation to start a pet project in F# :)

  • Map, Fold, and Filter are your friends. Try to express algorithms in these functions rather than implementing a loop with recursion.

  • Non-tail recursive functions are almost always easier to read and write. See here.

  • Project Euler. Lots of people recommend it, I didn't find it particularly helpful at all. You might get more use from it than me if your a mathematician, however.

  • <3 unions! Use them!

  • Falling back on mutable state -- big no-no. At least for beginners. The worst beginner code is full of mutables and ref variables. Immutability is an alien concept at first, so I recommend writing fully stateless programs for a while.

Still, the best advice is just keep coding everyday.

Hope that helps!

-- Juliet

Juliet
+1 Good post. I personally learn F# while solving problems on ProjectEuler. On ProjectEuler there are both problems which are easiest solved functionally and others which are easiest solved imperative.
lasseespeholt
Hi Juliet, thanks for the tips. Maybe I should also write some wikibooks, which may improve my understanding of the language/constructs while teaching others. I have already worked with Haskell some years ago in college; so I already have good notion of recursion (functions and data structures) and immutability. My problem with C++ was designing clear and clean libraries. I decided to modernize myself by learning modern programming languages, such as F#.
Allan
I'm a bit of a novice in F# too, but my answer would be Juliet's: "Keep coding everyday :)" Also, attempt at least once a week (if not everyday) to do something you don't already know how to do. That's how I learned every other programming language I know.
TechNeilogy
Juliet, the wikibook is awesome. Read it a while ago (well, not all of it), and very much liked the bits about fslex and fsyacc, which were very clear and easy to understand.
Alexander Rautenberg
+4  A: 

Hi Allan, you very probably don't want to hear from me as I have not achieved a high level of proficiency with F#, but I have set myself the goal of getting there (much like your self).

I thought I might suggest what has turned out to be my biggest learning tool: Stubbornness.

I set my self a large-ish project (a game, just as gradbot suggests). I then decided to code using as much immutable data as possible regardless of the performance costs.

Then if I couldn't find a way to use immutable data I'd come here and ask for help.

The hoops this stubborn approach forced me to jump through has been a brilliant learning exercise, as (just as Juliet mentions) F# allows you to write really ugly C# if you let your self get lax.

Right now I have tile based 2D world and a little man who can find his way to the treasure and back home again using A* path-finding ... The only thing I mutate is the title of the window it's displayed in.

I only got serious about learning F# towards the end of July (I'd dabbled before then) and this project has taught me a huge amount, along with the help of the guys here at StackOverflow.

jdoig
Hi jdoig, of course I want to hear from you. We are in the same boat! Good lucky in your development and thanks for the advice.
Allan
Thanks Allan, I was just being silly based on your opening line :¬) . Stick around here, these guys are excellent f#'ers.
jdoig
Robert Jeppesen
+5  A: 

I can only agree that trying to explain functional programming to others is a great way to learn it. I spent a lot of time about thinking the structure of my F# book and I think it really helped me to understand how functional concepts relate. Even giving a talk on F# in your company or to your friends should have a similar effect.

When I started learning F#, I started working on the F# WebTools project. I think this was quite useful, because many components of the project were perfect for functional programming, so I learned many functional tricks (because they were the best way to solve the problem). The project processed source code tree of F# and translated it to JavaScript, so I was using a lots of recursive functions and discriminated unions.

The area you're working in is quite different than my, so I cannot give you any specific advice, but it is a good idea to write programs in a clear functional way - even if you think that it would look nicer if you wrote it in the C++ style. When you write it, you'll probably find some way to simplify your code.

So, I think that the tips I could give are:

  • Try to explain F# to others - this helps you to organize ideas in your mind
  • Pick good problems to start with - e.g. algorithmic problems, processing tree structures, etc.
  • Write as much F# as you can and don't be afraid to start with a solution that does not look perfect to you - I rewrote my first program so many times!
Tomas Petricek
Hi Tomas, thanks a lot. It happens that I am always unsatisfied with my code. It is like if I was always trying to implement the "cleanest code" with a "perfect structure/design". This consumes me a lot of time! Maybe this will change with F#!
Allan
@Allan: I came to F# from C# and I know this feeling! I think that F# is more expressive, so you have better options for writing "perfect code". There are still some limiations, but you typically run into them less often. On the other hand, it is more difficult to learn all the best practices, because there is more flexibility (and also, there are fewer resources about them)
Tomas Petricek
+3  A: 

The other answers do a good job of suggesting ways to practice your F# writing skills, which is obviously very important. However, something which doesn't seem to have been emphasized much is reading F# code, which is just as important in my opinion. There are a lot of people doing cool stuff with F#, and one of the best ways to learn about the different corners of the language is to read about what they're doing. This will help you pick up idiomatic style and will also expose you to more design patterns, language features, and functional programming techniques than you would be likely to encounter if you were just solving a problem using F# on your own.

The F# blogs syndicated on Planet F# are a good place to start, but there are also several excellent books and presentations on the language.

kvb
You can pick up a lot from the F# standard library as well.
Jon Harrop
+3  A: 

This only answers part of your question, but as you talk about wanting to create libraries in F#, the F# component design guidelines just came out:

http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/fsharp-component-design-guidelines.pdf

Benjol
+3  A: 

If you are interested in scientific libraries I suggest you to take a look at Jon Harrop's F# for Scientists.

Also to catter for your mathematician side I suggest you to read Doets-Van Eijck The Haskell Road to Logic, Maths and Programming, although written in Haskell, you will certainly be able to follow most of the text, and re-implementing the samples in F# could be a nice exercise.

Edgar Sánchez
Thanks! Also, my latest book "Visual F# 2010 for Technical Computing" is much more up to date!
Jon Harrop