Over a million developers have joined DZone.

F# Possibly the Most Important New Release with VS 2010 and .NET 4

·
If you take a minute to look at Microsoft's white paper, "Why upgrade to Visual Studio 2010?", you'll see that it mentions Silverlight, Azure, WPF, Test-First development, and more.  What you won't find is any mention of Microsoft's new F# language, which debuted with the .NET Framework 4.  In fact, it's not mentioned in the entire paper.  So why is such a major technology in the future of .NET concurrency being under-advertised?  

F# is still a relatively young language, but it already has support from several corners of the tech world.  Ted Neward, an authority on Java and .NET technologies, is a huge supporter of F# and he's done several talks on the language already.  In this article Neward explains how F# came to be.

The core language of F# is heavily inspired by OCaml.  In fact, the language is basically a version of OCaml based on .NET's Common Language Runtime (CLR).  Neward said in the aforementioned article that OCaml by itself wasn't so great for a "line-of-business developer":  

"Building a UI with OCaml is awkward and difficult at best… It’s not that OCaml isn’t interesting-it is-but as it comes, out of the box (so to speak), OCaml isn’t something that the line-of-business developer can make use of in any sort of pragmatic sense."

However, Neward feels that business developers will use F# because it takes the OCaml features to the CLR platform, which is common in enterprises.  

IntelliFactory also has faith in the business-usability of F#, and believes that it can forge a path into the mainstream web application space.  Earlier this year they released their WebSharper platform for writing web apps in F# that are easily compiled into JavaScript for the client-side.  jQuery, qooxdoo, Flapjax, and Yahoo UI are all supported by WebSharper.  WebSharper comes fully integrated into VS 2010.

One possible explanation for the lack of F# advertising is that it is still considered a niche language by many, and many developers have not heard about it.  Financial institutions and quantitative researchers are attracted to its parallel and algorithmic programming, but the concurrency features should really help F# go mainstream.  Eventually, a majority of programs will have to harness the increasing number of cores going into modern hardware.  Financial institutions are, as usual, the early adopters of these kinds of high-performance technologies.  Developers who want to get ahead of the F# curve can also become early adopters by getting started with the Essential F# refcard.  The card was authored by Chance Coble and Ted Neward.

F# Examples
F# is a three-paradigm language that uses imperative, functional, and object-oriented programming (class-based).  New programming design approaches are possible with F# that are not easily expressed solely through objects.  The functional programming capabilities can also lead to some mind-bending code, says Neward.  F# is a strongly typed language that uses type interface so that data types don't need to be explicitly declared.  Take a look at these recursive functions and check out Microsoft's F# Developer Center for comprehensive documentation on F#.
(* print a list of numbers recursively *)
let rec printList lst =
match lst with
| [] -> ()
| h :: t ->
printf "%d\n" h
printList t

(* Same thing, using matching against list elements *)
let rec printList2 l =
match l with
| [] -> ()
| h :: t -> printfn "%A" h
printList2 t

(* Using shorthand for match *)
let rec printList3 = function
| [] -> ()
| h :: t -> printfn "%A" h
printList3 t

(* Or, using a higher-order function *)
let printlist4 lst = List.iter (printfn "%A") lst

(* Sample Windows Forms Program *)

(* We need to open the System library for the STAThreadAttribute class *)
open System

(* We need to open the Windows Forms library *)
open System.Windows.Forms

(* Create a window and set a few properties *)
let form = new Form(Visible=true, TopMost=true, Text="Welcome to F#")

(* Create a label to show some text in the form *)
let label =
let temp = new Label()
let x = 3 + (4 * 5)
(* Set the value of the Text*)
temp.Text <- sprintf "x = %d" x
(* Remember to return a value! *)
temp

(* Add the label to the form *)
form.Controls.Add(label)

(* Finally, run the form *)
[<STAThread>]
Application.Run(form)

(* Fibonacci Number formula *)
let rec fib n =
match n with
| 0 | 1 -> n
| _ -> fib (n - 1) + fib (n - 2)

(* An alternative approach - a lazy recursive sequence of Fibonacci numbers *)
let rec fibs = seq {
yield! [1; 1];
for (x, y) in Seq.zip fibs (Seq.skip 1 fibs) -> x + y }

(* Print even fibs *)
[1 .. 10]
|> List.map fib
|> List.filter (fun n -> (n % 2) = 0)
|> printlist

(* Same thing, using sequence expressions *)
[ for i in 1..10 do
let r = fib i
if r % 2 = 0 then yield r ]
|> printlist

(* Async workflows sample (parallel CPU and I/O tasks) *)

(* A very naive prime number detector *)
let isPrime (n:int) =
let bound = int (System.Math.Sqrt(float n))
seq {2 .. bound} |> Seq.exists (fun x -> n % x = 0) |> not

(* We are using async workflows *)
let primeAsync n =
async { return (n, isPrime n) }

(* Return primes between m and n using multiple threads *)
let primes m n =
seq {m .. n}
|> Seq.map primeAsync
|> Async.Parallel
|> Async.RunSynchronously
|> Array.filter snd
|> Array.map fst

(* Run a test *)
primes 1000000 1002000
|> Array.iter (printfn "%d")
Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}