views:

268

answers:

7

I would like to learn F#, mentally wise, it is a different way of programming, how would you shift away from the OOP paradigm in learning the functional aspects of the language itself?

I know I run the risk of getting this closed as it is subjective...

Any tips/advice would be appreciated.

Best regards, Tom.

+8  A: 

Okay, this is a blatant plug, but it's certainly relevant...

Tomas Petricek has a book which I helped out on called Real-World Functional Programming. The point of the book is to teach the functional programming concepts to developers familiar with OOP - via F# and C#. It sounds like it's exactly the kind of thing you need to get an FP mindset.

Jon Skeet
@Jon: Ahhh cool.... cheers for the link... :)
tommieb75
I'll add that even though I do Haskell and OCaml development, it is a fantastic book. It you want to make the mental shift, buy the book and read all the way through.
Shaun
It's a good book. Definitely worth it.
Joel Mueller
Got the book too, it reaches the parts...
Benjol
+1  A: 

Do you know any other functional languages? I think the best thing you can do is keep your mind open and not try to constantly compare to any rigid OO paradigms you already know. Accept the differences.

Or, like a guy in my LISP class once tried, get baked out of your mind before starting. ;) Not sure that actually worked though.

FrustratedWithFormsDesigner
No I don't actually, I thought, the world is my oyster and decided to learn functional programming... :)
tommieb75
@tommieb75: Well good for you! Learning new techniques and styles will give you new ideas and you'll be able to see new solutions that you might never have thought of before.
FrustratedWithFormsDesigner
+3  A: 

Javascript is also a functional language. Take a look at Douglas Crockford's Javascript series where he covers the design patterns and tips/tricks that apply to any functional language.

http://blog.flashmech.net/2009/05/douglas-crockfords-javascript-video-series/

spoulson
@Spoulson: Really? Javascript? you're having me on right? Interesting.. then why was it not declared as 'functional' instead of a web-hack programming language?
tommieb75
+1 from me for pulling this one off...you should post a question along the lines of like 'Javascript is a functional language...discuss?' :)
tommieb75
Javascript is a great functional language. Your gripe is likely associated with varying compatibility between browser DOM implementations.
spoulson
@tommieb75: Also check out this MIT video lecture series that covers LISP. The first lecture dives deep in fundamental functional programming with LISP. This would also be a great primer to functional concepts.http://groups.csail.mit.edu/mac/classes/6.001/abelson-sussman-lectures/
spoulson
Javascript is Scheme in C clothing.
Bruno Reis
+4  A: 

Since someone mentioned lisp it reminded me of the SICP lectures of Hal Abelson and Gerald Jay Sussman. Although it is lisp, you'll encounter many semantic similarities. In a number of the lectures they talk about thinking in a functional way. I don't think they come out and say it explicitly, but much of the class I recall is devoted to it. Some topics he covers that are pertainent,

  1. composition of functions / first class functions
  2. use functions as black-boxes (to his point of view)
  3. recursion
  4. pattern matching
  5. abstracting similar patterns from a few functions to create general functions

Those are the topics I remember, I'm sure there are more that should put you in a good mindset. All the videos can be found on the SICP web site and google video. enjoy!

nlucaroni
+1  A: 

I've learned every sort of programming I know by doing it. Start small, maybe by altering existing F# programs, certainly not by writing anything large. Slowly build up. You'll have the concepts down long before you're really good at writing according to the concepts.

Books and tutorials can be useful, but nothing will replace getting some practice at functional programming. What would be nice would be a mentor, but nowadays if you're stuck on something you can always ask on SO.

Don't get too ambitious in your time estimates either. You can learn a new language fast, but learning to think in a new way is a lot slower. Accept that you're likely to get confused and puzzled, but know you can get out of it. (Then again, maybe you'll take to it like a cat to catnip and have no problems with it. It's never happened to me, but my experience isn't universal.)

And have fun learning.

David Thornley
+4  A: 

There are a variety of questions in the functional-programming tag that give general advice along the same lines, like

http://stackoverflow.com/questions/345792/learning-functional-programming

http://stackoverflow.com/questions/2399/how-to-transition-to-functional-programming

For F#, there are a number of possible approaches; two relative extremes along one continuum are

  • the 'cold turkey' approach, where you try to forget/unlearn everything you know and start with beginner's tutorials/books/blogs for learning the language
  • the 'slowly inching into the cold swimming pool' approach, where you start with familiar simple programs/technologies/tasks (e.g. small C#/VB programs if you're a .Net programmer) and translate them into F# to start learning the syntax and the tooling; the as you get more accustomed to the environment/language, start slowly trying to adopt new idioms and structures of the language

There are trade-offs either way, and most of the advice I've seen suggests that FP is different enough from OOP that people like to recommend something closer to the first bullet rather than the second one.

In any case, experience is a good teacher, so just get in and start writing small programs (or inspecting the small programs of others that you'll find on blogs, etc.). As for F# resources,

http://stackoverflow.com/questions/734525/getting-started-with-f

is a good starting point. And I'd be remiss not to include a link to my blog entry "Brian's favorite online content for learning F#".

Brian
@Brian: Does the CTP preview of F# screw with VS 2008 with SP1? The last thing I want is to break the fragility of VS2008 - you know how it can be...temperamental... :) Thanks for your answer...
tommieb75
You should be good; a number of us on the team run the CTP on 2008 on our own boxes (and the CTP uninstall story is decent now, for in case you do run into any trouble).
Brian
A: 

Write a Scheme interpreter. Start with the previous specification (R5RS) which is shorter and simpler than the new one (R6RS). Afterwards, functional programming will be easy; you will be ready to tackle the other hard part of F#, which is type inference.

(Yes, I am serious. Scheme is quite adequate for learning functional language internals, and writing an interpreter for a language is, in my experience, the best way to master it. Having a clear and short specification makes it easier, and the Scheme specification is really shorter than F# specification.)

Thomas Pornin
@Thomas: My first thought is are you serious? :) Really, I am just getting a bit wet behind the ears on this....
tommieb75