• If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!


F Sharp

This version was saved 15 years, 2 months ago View current version     Page history
Saved by David Morton
on April 6, 2009 at 10:26:56 am

"let add x = x + 1"


What is F#?


F# is a functional language based loosely on OCaml.  While it is not a purely functional language, in that it also contains elements of imperative programming, it is mainly designed for functional programming, and encourages the programmer to write code that is functional, by making the process simpler than many object-oriented languages such as C#, C++ and VB.  It is still considered part of Microsoft Research currently, but will soon be a full-fledged programming language alongside the regulars in Visual Studio 2010. 


What is functional programming?


Functional programming is a style of programming "without side effects".  It's been around for over 50 years now, but is making a resurgence mainly because of it's applicability to multi-core and multithreaded environments.  As there are no side effects in pure functional programming, functional applications are inherently thread-safe. 


Why use F#?


In short, the encouragement to program without side effects can help to create cleaner and more threadable code.  Also, it's application to problems of mathematics, as well as the simplicity it offers to the problem of asynchronous processing make it an ideal language for programming for multi-core processors.  F#, while not being a pure functional language still provides several mechanisms for interacting with the .NET Framework, and all code compiled with F# still compiles into basic IL instructions, making it suitable for being used in .NET applications.  


What are some examples of F#?


The simplest example could be something like the following:


let add x y = x + y


The above function is called "add", and takes two parameters, x and y.  These two parameters are added together on the right side of the function and output as the result of the function call.  In F#, all types are inferred, and where a variables type cannot be inferred from it's usage, the variable defaults to Int32.  Should the default to Int32 raise other issues with the code, you can specify the type using the following syntax:


let add (x:float) (y:float) = x + y


The above example specifies the type of x and y.


In functional programming, the function is the central unit of the code, and not the object, as is the case with object-oriented programming.  Language constructs for F# make it much simpler to "derive" functions from other functions.  Here's another function that's based off of the above "add" function.


let add x y = x + y

let addToOne x = add 1 x


This new function, addToOne, simply builds upon the functionality of the add function and specifies one of the values.  The C# equivalent to the above code might look like this:


public int add(int x, int y)


     return x + y;



public int addToOne(int x)


     return add(1, x); 



Notice the simplicity and brevity of the F# statement as opposed to the C# statement.  One of the keys to writing functional programs is to keep the function size small and simple.  Functions should be built using function composition, building larger functions from smaller ones.  This, combined with the side-effect free nature of functional programming, can enable you to make giant changes to your code base without much risk of affecting the functionality of other parts of your code. 


Lastly, let's take a quick look at function composition.  Here's the code:


let add x y = x + y

let addToOne x = add 1 x

let addToTwo x = add 2 x

let addToThree = addToOne >> addToTwo

0 |> addToThree |> printfn "%A"


This code block will print "3" at it's end.  Here's how it works, line by line:


The first line is our simple add method, adding the two values x and y.

The second line is the derived function that adds one to x.

The third line is like the second, only adding 2 instead of 1.

The fourth line is the composition of addToOne and addToTwo.  This is the "forward composition" operator.  The C# equivalent may look like this:


public static int addToThree(int x)


    return addToTwo(addToOne(x));



The fourth line is actually calling this method using the pipeline operator.  It's passing "0" into addToThree, and taking the result of that function, and printing it to the command line using the printfn function.  It could also have been written as:


printfn "%A" (addToThree 0)


But the clarity to be afforded by the pipeline is preferred. 

Comments (0)

You don't have permission to comment on this page.