share
Stack OverflowGetting started with F#
[+165] [15] Dead account
[2009-04-09 14:39:19]
[ .net f# getting-started ]
[ http://stackoverflow.com/questions/734525] [DELETED]

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.

(2) +1 because it's a 'nice question' - JohnIdol
(1) +1 because it must be the highest voted F# question. - claws
(5) +1 because your surname is Quigley. - Stimul8d
(5) This is StackOverflow. Once you ask a question, it's no longer yours. - codekaizen
(2) @Ian well if they do delete your account for you, you can't stop us putting it all back afterwards :-p But please reconsider. - Rup
(2) Well, Ian, I'm sorry that you are having such a hard time participating in this adult community. I'm really not bothered if you use all your reputation to downvote all of my posts. I'm sure you'll create some lasting memories doing so... - codekaizen
(7) @Ian: this post (and many others that you have initiated) contain a lot of content that was contributed by other people in addition to yourself; I don't think you have the right to unilaterally demand that it is removed or made unusable. Don't you think? - CesarGon
(6) @Ian: If you want your account deleted, send an email to team@stackoverflow.com from the email address listed in your profile. - Bill the Lizard
(1) +0 for vandalizing your own question (WTF?) - Mark
(28) ...what on earth happened here? - Zeus
Ian - the Ghost - Henrik
[+177] [2009-04-09 14:42:16] Tarkus [ACCEPTED]

Entry point is here -> Microsoft F# Developer Center [1]

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:

  1. Programming F#: A comprehensive guide for writing simple code to solve complex problems [4] by Chris Smith
  2. Expert F# 2.0 (Expert's Voice in F#) [5] by Don Syme, Adam Granicz, and Antonio Cisternino

And in addition to that:

Quick Links:

Community

Blogs

Videos:

Good Books:

Samples

Hello World Samples:

Sample 1 [52], Sample 2 [53], Sample 3 [54], Sample 4 [55], Fibonacci Numbers [56], Download Stock Quotes [57]

Code Sample:

// 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.

Tools needed:

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.

MonoDevelop:

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/hh388569
[2] http://www.tryfsharp.org
[3] http://channel9.msdn.com/pdc2008/TL11/
[4] http://rads.stackoverflow.com/amzn/click/0596153643
[5] http://rads.stackoverflow.com/amzn/click/1430224312
[6] http://www.microsoft.com/downloads/en/details.aspx?FamilyID=effc5bc4-c3df-4172-ad1c-bc62935861c5&displaylang=en
[7] http://blogs.msdn.com/b/dsyme/archive/2010/11/10/november-2010-f-2-0-free-tools-update.aspx
[8] http://www.microsoft.com/visualstudio/en-us/
[9] http://www.microsoft.com/downloads/details.aspx?FamilyID=f8c623ae-aef6-4a06-a185-05f59be47d67&displaylang=en
[10] http://blogs.msdn.com/b/dsyme/archive/2010/08/17/announcing-the-f-2-0-free-tools-for-net-4-0.aspx
[11] http://www.microsoft.com/visualstudio/en-us/
[12] http://msdn.microsoft.com/en-us/fsharp/cc835246.aspx
[13] http://research.microsoft.com/fsharp/manual/spec2.aspx
[14] http://research.microsoft.com/fsharp/manual/spec.pdf
[15] http://msdn.microsoft.com/en-us/library/dd233154%28VS.100%29.aspx
[16] http://msdn.microsoft.com/en-us/library/dd233181%28VS.100%29.aspx
[17] http://msdn.microsoft.com/en-us/library/ee353567%28VS.100%29.aspx
[18] http://en.wikibooks.org/wiki/Programming%3aF_Sharp
[19] http://en.wikipedia.org/wiki/F_Sharp_programming_language
[20] http://code.msdn.microsoft.com/Project/ProjectDirectory.aspx?TagName=F%23
[21] http://code.msdn.microsoft.com/fsharpsamples
[22] http://www.developerfusion.com/article/122079/intro-to-f-programming/
[23] http://tryfsharporg.cloudapp.net/Tutorials.aspx
[24] http://stackoverflow.com/questions/tagged/f%23
[25] http://cs.hubfs.net/forums/
[26] http://feeds.feedburner.com/planet_fsharp
[27] http://fsharpcentral.com/
[28] http://twitter.com/search?q=%23fsharp
[29] http://paper.li/tag/fsharp
[30] https://github.com/fsharp/
[31] http://www.codeplex.com/site/search?TagName=F%23
[32] http://fpound.net/
[33] http://blogs.msdn.com/dsyme/
[34] http://strangelights.com/blog/
[35] http://cs.hubfs.net/blogs/default.aspx
[36] http://fsharpnews.blogspot.com/
[37] http://tomasp.net/blog/
[38] http://lorgonblog.spaces.live.com/blog/
[39] http://channel9.msdn.com/pdc2008/TL11/
[40] http://channel9.msdn.com/posts/martinesmann/Don-Syme-FSharp-and-functional-programming-in-NET/
[41] http://channel9.msdn.com/posts/Dan/C9-Bytes-Data-Visualization-and-FSharp-with-Luke-Hoban/
[42] http://channel9.msdn.com/posts/Charles/Don-Syme-Whats-new-in-F-Asynchronous-Workflows-and-welcome-to-the-NET-family/
[43] http://channel9.msdn.com/posts/Charles/Luke-Hoban-Latest-version-of-F-Released-Whats-the-story-Whats-next/
[44] http://channel9.msdn.com/Search/Default.aspx?Term=F%23&Type=c9
[45] http://msdn.microsoft.com/en-us/fsharp/ff759495.aspx
[46] http://rads.stackoverflow.com/amzn/click/1590597575
[47] http://rads.stackoverflow.com/amzn/click/1590598504
[48] http://rads.stackoverflow.com/amzn/click/1430223898
[49] http://www.ffconsultancy.com/products/fsharp_for_technical_computing/
[50] http://rads.stackoverflow.com/amzn/click/1933988924
[51] http://oreilly.com/catalog/9780596153656
[52] http://fsharpdotnet.com/
[53] http://www.fsharphelp.com/HelloWorld.aspx
[54] http://sharp-gamedev.blogspot.com/2008/09/hello-world.html
[55] http://techiethings.blogspot.com/2009/02/f-hello-world.html
[56] http://mbishop.esoteriq.org/weblog/?p=7
[57] http://www.fsguy.com/2010-08-24_how-to-download-stock-quotes-from-google-finance-with-fsharp
[58] http://www.microsoft.com/downloads/details.aspx?FamilyID=40646580-97FA-4698-B65F-620D4B4B1ED7
[59] http://www.microsoft.com/downloads/details.aspx?FamilyID=8e5aa7b6-8436-43f0-b778-00c3bca733d3&displaylang=en
[60] http://functional-variations.net/monodevelop/
[61] http://tirania.org/blog/archive/2011/Feb-16-1.html

+1. Thanks. Your editted answer breaks it down into bite sized chunks - Dead account
+1: Informative post, useful beginner code too :) - Juliet
@Ian Quigley, you're welcome! :-) - Tarkus
(3) +F, +A. outstanding value for money! - Dead account
+1 Are you serious.. this list is sickening. - Sung
Thanks for sharing this info so concisely. It really help to get started - Shreedhar
Wow Brian...what a list...a great way to up your rep huh...nonetheless +1 from me .... :) feelin' good already.... - t0mm13b
+1 Detailed and informative answer - Holystream
Great answer. Spot the bug in sayHelloDotNet. :) - Robert Jeppesen
(1) tools: Add LinqPad. It is free and can be used for run simple F# app - Liang Wu
1
[+29] [2009-04-09 14:50:34] Juliet

F# 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_Sharp
[2] http://stackoverflow.com/questions/694651/what-task-is-best-done-in-a-functional-programming-style/694822#694822
[3] http://en.wikibooks.org/wiki/F_Sharp_Programming/Lexing_and_Parsing
[4] http://en.wikibooks.org/wiki/F_Sharp_Programming/MailboxProcessor

(2) +1, I've started looking at the code while installing... I think it's going to make my head hurt more than Linq - Dead account
+1 I am reading F# on wikibooks.. - Sung
@Ian Quigley, I can confirm that! - Benjol
Not sure whether to up-vote here, for plugging ones own book (well, you wrote most of it)! :P Saying that, I skimmed over a good deal of it, and found it to be pretty well written. (Fixed one or two issues of grammar too.) - Noldorin
(5) You mean "in even four times the lines of code"? :-) - Ken
2
[+21] [2009-04-09 16:25:39] David Klein

I'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/

(1) I've been reading this book, it is pretty good so far. - Jamie Penney
3
[+13] [2009-04-09 15:13:24] Tab

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/


+1. Saw a presentation with Luca once. He's got a great accent :) - Dead account
i agree, this video was what made me look into F# in the first place :) - Alex
4
[+12] [2009-04-09 15:08:19] thinkhard

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.aspx
[2] http://cs.hubfs.net/forums/default.aspx
[3] http://blogs.msdn.com/chrsmith/
[4] http://blogs.msdn.com/dsyme/
[5] http://books.google.com/books?id=NcrMkjVxahMC&printsec=frontcover&dq=Expert+F%23&ei=QwveSYffNY%5FCzASrjoi6Dg
[6] http://www.ffconsultancy.com/
[7] http://books.google.com/books?id=wu0yHwAACAAJ&dq=F%23+for+Scientists&ei=AAzeSdkng8jIBP2q-KsO
[8] http://strangelights.com/blog/default.aspx
[9] http://books.google.com/books?id=n1DEBdl%5FoloC&pg=PP1&dq=Foundations+of+F%23&ei=eQveSaPsL4PIyAT9qvirDg
[10] http://rads.stackoverflow.com/amzn/click/1430223898

Light syntax is now enabled by default, and I believe it's traditional for a "hello world" program to output a trailing newline, so I'd consider changing the code to just printfn "hello world". :P - bcat
5
[+11] [2010-06-22 12:31:06] Benjol

I just happened upon an interesting-looking online book here:

The F# Survival Guide [1]

[1] http://www.ctocorner.com/fsharp/book/default.aspx

Noted..reading that one! Thanks for the link, it looks like a REALLY good start. - Stimul8d
This Link is dead and it is impossible de find it anywhere else except into the web cache:web.archive.org/web/20110715231625/http://www.ctocorner.com/… - Nicolas GUILLAUME
6
[+6] [2010-04-24 14:32:33] Damian Powell

This 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/

totally agree about that... but I think you might even go farther and start with Scheme or LISP, i.e. reading SICP. - Kevin Won
(2) +1 Get started in F# by learning functional programming in a pure FP language. Being so easy to do imperative programming in F# gets in the way of learning the functional side of it. - Tony Lee
7
[+5] [2011-01-27 15:06:22] Cygwin98

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.


8
[+5] [2009-12-19 20:42:34] John

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.


9
[+3] [2009-04-17 09:47:35] Carsten König

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.aspx

10
[+3] [2009-11-12 18:09:51] mloskot

I 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:

This 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.

[1] http://tomasp.net/articles/fsharp-i-introduction/article.pdf
[2] http://tomasp.net/blog

11
[+2] [2009-04-10 13:37:05] flatline

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.


12
[+2] [2010-09-04 14:44:52] 2sharp

A new blog with some F# tutorials is here: http://2sharp4u.wordpress.com/


13
[+2] [2010-04-24 14:06:40] Arif

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)

Please, stop that. - Will
14
[+1] [2010-01-29 17:14:17] Brian

Brian's favorite online content for learning F# [1]

[1] http://lorgonblog.spaces.live.com/blog/cns!701679AD17B6D310!1325.entry

15