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.
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.
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:
And in addition to that:
Sample 1, Sample 2, Sample 3, Sample 4, Fibonacci Numbers, Download Stock Quotes
// 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, 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.
There is a terrible introduction video for F# on DNRtv. That should turn any C# coder off completely.
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.
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)"
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.
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.
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.
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)
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.
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.
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)
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.
If you want a procedural programmers intro to F# and to functional thinking try: Guide to F#
A new blog with some F# tutorials is here: http://2sharp4u.wordpress.com/