views:

7232

answers:

16

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.

+129  A: 

Entry point is here -> Microsoft F# Developer Center

Start by watching videos and presentations (BTW, An Introduction to Microsoft F# 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 by Chris Smith
  2. Expert F# 2.0 (Expert's Voice in F#) by Don Syme, Adam Granicz, and Antonio Cisternino

And in addition to that:

Quick Links:

Blogs

Videos:

Hello World Samples:

Sample 1, Sample 2, Sample 3, Sample 4, Fibonacci Numbers, Download Stock Quotes

Good Books:

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:

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, linked from the F# download page), or the VS2010 integrated shell (free download). Then install the CTP MSI.

Or, of course, get a full version of VS2010, where F# is built-in.

Koistya Navin
+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! :-)
Koistya Navin
+F, +A. outstanding value for money!
Dead account
+1 Are you serious.. this list is sickening.
Sung Meister
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....
tommieb75
+1 Detailed and informative answer
Holystream
Great answer. Spot the bug in sayHelloDotNet. :)
Robert Jeppesen
A: 

There is a terrible introduction video for F# on DNRtv. That should turn any C# coder off completely.

leppie
Can you provide a link?
zendar
I would have to google for it, so you can do that too :)
leppie
http://www.dnrtv.com/default.aspx?showNum=136
Leyu
+19  A: 

F# on Wikibooks 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 describing simple symbolic logic in F# and C#, this simple SQL parser in 150 lines of code, and a prime number sieve using mailbox processors. Its not possible to write any of these samples in C# in even a quarter of the lines of code.

Juliet
+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 Meister
@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
You mean "in even four times the lines of code"? :-)
Ken
+8  A: 

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 contains many links to get you started. A good forum for questions dealing specifically with F# is HubFS

Chris Smith has an excellent series on F# comparing C# implementations with their F# equivalents (dare I say betters?)

Don Syme, the father of F# has a weblog on the subject as well as a book out entitled Expert F#

Jon Harrop has a book entitled F# for Scientists

Robert Pickering is the author of Foundations of F# with its second edition available for pre-order on Amazon.com, it has been renamed to Beginning F# 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)"

thinkhard
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  A: 

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/

Tab
+1. Saw a presentation with Luca once. He's got a great accent :)
Dead account
+12  A: 

I'd also recommend checking out Real World Functional Programming 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.

David Klein
I've been reading this book, it is pretty good so far.
Jamie Penney
A: 

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.

flatline
+1  A: 

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 (it more or less a collection of good blogs/sources and a good forum)

CKoenig
+2  A: 

I also recommend F# Language Overview (PDF) written by Tomáš Petříček 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.

mloskot
+3  A: 

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.

John
+1  A: 

Good link: Arif's Cyber Space

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)
Arif
+2  A: 

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.

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.

Damian Powell
totally agree about that... but I think you might even go farther and start with Scheme or LISP, i.e. reading SICP.
Kevin Won
+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
A: 

If you want a procedural programmers intro to F# and to functional thinking try: Guide to F#

mikej
+4  A: 

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

The F# Survival Guide

Benjol
Noted..reading that one! Thanks for the link, it looks like a REALLY good start.
Stimul8d
+1  A: 

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

2sharp