What's a good way to get into F# programming?
What's a good "Hello world" example and what simple examples can show me why I want to use it over C#.
Also what tools do I need? I have WindowsXP, Visual Studio 2008 etc.
For a quick taste, consider trying F# in your browser [2].
Start by watching videos and presentations (BTW, An Introduction to Microsoft F# [3] by Luca Bolognese is one of the best presentations on the subject). Then read the following two must-read books:
And in addition to that:
Sample 1 [52], Sample 2 [53], Sample 3 [54], Sample 4 [55], Fibonacci Numbers [56], Download Stock Quotes [57]
// C# :
// using System;
open System
// say hello wrold
printfn "Hello, World! What is your name, user?"
// C# :
// var name = Console.ReadLine();
let name = Console.ReadLine()
// C# :
// public delegate void SaySomethingDelegate(string toWho);
// SaySomethingDelegatesayHello =
// who => Console.WriteLine("Hello, {0}!", who);
let sayHello who = printfn "Hello, %s!" who
// hi
sayHello name
// you can using .NET Framework classes and methods:
let sayHelloDotNet who =
Console.WriteLine("Hello from F# via .Net, " + name + "!")
// hello again!
sayHelloDotNet name
// let's count Fibonacci
let rec fib i =
match i with
| 1 | 2 -> 1
| i -> fib(i-1) + fib(i-2)
// result
printfn "%i" (fib 20)
Note that there is also a "getting started" small sample as a 'tutorial' project template inside Visual Studio.
If you want F# integrated into Visual Studio, then you either need a non-express version of VS2008, or the VS2008 shell (integrated mode; this component is also a free download [58], linked from the F# download page), or the VS2010 integrated shell ( free download [59]). Then install the CTP MSI.
Or, of course, get a full version of VS2010, where F# is built-in.
You can find lots of information about using F# within MonoDevelop here [60]. The F# compiler and fsi.exe are now part [61] of the Mono distribution.
[1] http://msdn.microsoft.com/en-us/vstudio/hh388569F# on Wikibooks [1] is probably the best online resource for beginners wanting to learn F#. Plus its free (as in "free beer"). Feel free to fix all of my spelling/grammar errors as you go through it :)
What's a good "Hello world" example and what simple examples can show me why I want to use it over C#.
If you don't already know F#, its really hard to post an example comparing F# and C# together and say "ah ha! Now you see why this language is so much better" since you don't really know the syntax. But, for what its worth, I wrote this post [2] describing simple symbolic logic in F# and C#, this simple SQL parser [3] in 150 lines of code, and a prime number sieve using mailbox processors [4]. Its not possible to write any of these samples in C# in even a quarter of the lines of code.
[1] http://en.wikibooks.org/wiki/Programming:F_SharpI'd also recommend checking out Real World Functional Programming [1] from Tomas Petricek and Jon Skeet. (Early Access Edition already available) It covers various problems and shows how to tackle them efficiently in a functional way. Most of the Code is in F# but also in C# so you can compare which you like better/you think fits better.
[1] http://manning.com/petricek/I won't duplicate all the great info above but I wanted to added a link to this session with Luca Bolognese. His presentation was informative, engaging and cleared up a lot of misconceptions I had about F# - certainly one of many great resources.
http://channel9.msdn.com/pdc2008/TL11/
A good way to get into F# is to just start doing things. There are many web resources to begin learning F# from scratch. There are 3 published books on the topic as well.
Microsoft F# Developer Center [1] contains many links to get you started. A good forum for questions dealing specifically with F# is HubFS [2]
Chris Smith [3] has an excellent series on F# comparing C# implementations with their F# equivalents (dare I say betters?)
Don Syme [4], the father of F# has a weblog on the subject as well as a book out entitled Expert F# [5]
Jon Harrop [6] has a book entitled F# for Scientists [7]
Robert Pickering [8] is the author of Foundations of F# [9] with its second edition available for pre-order on Amazon.com, it has been renamed to Beginning F# [10] in this edition to differentiate between the other books available.
As you requested:
printfn "hello world"
This does little to elucidate the powerful features available in the language. F# is an amalgamation of imperative, functional, and object-oriented paradigms... allowing you to smoothly transition between these approaches when crafting code. As far as I can tell F# is gaining momentum, it is slated to become a 1st class .NET language and is built into Visual Studio 2010. I run the September 2008 CTP release of F# from my windows workstations and FSharp-1.9.4.19 on linux with mono. I for one am investing a considerable precentage of my time to mastering the language.
As to why you would want to use it over C#. Thats a very good question. There are differences in the way F# compiles and C# compiles, reference some other questions on stackoverflow for good answers. I've generally noticed many naively claim that F# is simply "syntactic sugar", this is only half true.
"You obviously cannot write code in either language that compiles to the same bytecode because F# generates ILX (e.g. tail calls) and C# does not. F# also makes extensive use of CIL metadata for things that C# does not support (e.g. inlining). – Jon Harrop (Mar 14 at 7:06)"
[1] http://msdn.microsoft.com/en-us/fsharp/default.aspxprintfn "hello world"
. :P - bcat
I just happened upon an interesting-looking online book here:
[1] http://www.ctocorner.com/fsharp/book/default.aspxThis might sound like a strange answer to the question "What's a good way to get into F# programming?" but stay with me!
If you want to get started with the aspects of F# that make it distinct from C# and VB, I would suggest spending some time getting to grips with Haskell - and you can even do it without reading a book! Check out Erik Meijer's C9 Lectures series on Functional Programming Fundamentals [1].
After getting about half way through, I took another look at F# (after several prior false starts) and found that the penny finally dropped. The syntax of F# made more sense after understanding a bit more about its roots. Haskell is a pure functional language with such a clean syntax that it helps in understanding the foundations without that syntax getting in the way.
[1] http://channel9.msdn.com/shows/Going+Deep/Lecture-Series-Erik-Meijer-Functional-Programming-Fundamentals-Chapter-1/The best way to learn programming is programming, a lot. Project Euler (projecteuler.net) is a good start. To solve project euler problems, you can practice algorithms and basic data structures. To give yourself a bit more motivation, you can try to compete in some programming contests such as Google Code Jam, Facebook Hacker Cup.
Since I give my suggestions already, I think I might as well share a bit experience here.
For Project Euler problems, you really need to gradually build up your arsenals --- code snippets to do certain things, so you're not re-invent the wheels every time, which is very distracting and frustrating. For example, you may need functions to generate prime numbers, test prime numbers, generate combinations/permutations, factorization, etc. Some features I really like in F# is that bigint and BigRational, basically unlimited precision integer and rational numbers, saved me numerous times. The other advantage of F# is that you can do functional solutions first, if you need to optimize, you can evolved it into an imperative solution like what you can do in C/C++. Sometimes I do get frustrated with some features such as inflexible for loop, while loops, which makes checking loop conditions much more verbose. As there is no time constraint in Project Euler (unless you want to be the first ones to solve new problems), you can take time to think of elegant solutions. Despite of numerous complaints upon the solutions page on Haskell wiki site, I find it very helpful if I search for a better and more functional solution.
Coding competitions are totally different animals. Over there, you have to solve as many problems as possible in two or three hours. So you need to have a really good arsenal, not only data structures, but also some common algorithms, such as graph theory algorithms, dynamic programming, computational geometry, etc. As F# is relatively new and less popular language, and even less people use it in coding competition, online coding contests-related F# resource is particularly rare. Numerous time I find myself try to translate a C/C++/Java algorithm during a competition and get time expired message. That can be frustrating sometimes. So, be well prepared in advance.
For my money (free!), I am liking the The F# Survival Guide. It's a solid introduction to functional programming via F#. It lives at http://www.ctocorner.com/fsharp/book.
I still belive Expert F# is the best publication avaiable to get into F# and it covers almost everything.
The only problem is: like every newer "learn programming" book it don't contain a single excercise - so you will need to find something to do for yourself.
I recommend starting to play with Lists, the |> operator etc. and looking at the F# Developer Center [1] (it more or less a collection of good blogs/sources and a good forum)
[1] http://msdn.microsoft.com/en-gb/fsharp/default.aspxI also recommend F# Language Overview [1] (PDF) written by Tomáš Petříček [2] It is the best F# paper I've read from short articles. Its first paragraph says:
[1] http://tomasp.net/articles/fsharp-i-introduction/article.pdfThis text is based on a short overview of the F# language (...) The goal of this article is to introduce all the features in a single (relatively short) text, which means that understanding of a few advanced topics discussed later in the text may require some additional knowledge or previous experience with functional programming.
Navin's post outlines the options pretty well. I'm just beginning to learn the language but can offer some reflections from my personal experience.
I couldn't get through Foundations/Beginning, it may be a good reference but it reads like a reference and glosses over too much, early on. I'm enjoying Expert F# so far, it's a much better read IMO and the later chapters have some substance to them.
If you have little or no experience with functional programming, I recommend studying the subject more generally. If you want to make full use of what F# offers, over imperative C# coding, Structure and Interpretation of Computer Programs is a classic text that goes over a lot of FP concepts (I'd recommend just watching the videos). It uses Scheme as a teaching tool but it's dead simple, the concepts are easily portable to any other language that supports functional paradigms (including C#).
For more functional programming, Project Euler is a good place to actually apply pure functional programming concepts, since they are particularly well-suited to mathematical applications. I like this site if I'm trying out the syntax of a new language, it gives you something to work on, which is often the hardest part of learning something for practical use outside of work.
A new blog with some F# tutorials is here: http://2sharp4u.wordpress.com/
Good Books:
* Foundations of F# (Expert's Voice in .Net) by Robert Pickering (May, 2007)
* Expert F# (Expert's Voice in .Net) by Don Syme (Dec, 2007)
* Beginning F# by Robert Pickering (coming)
* F# For Scientists by Dr Jon Harrop (Aug, 2008)
* The Definitive Guide to F# by Don Syme (coming)
* Real World Functional Programming by Tomas Petricek
* Programming F# by Chris Smith (Oct, 2009)
Brian's favorite online content for learning F# [1]
[1] http://lorgonblog.spaces.live.com/blog/cns!701679AD17B6D310!1325.entry