F Sharp Programming/Values and Functions

Compared to other .NET languages such as C# and VB.Net, F# has a somewhat terse and minimalistic syntax. To follow along in this tutorial, open F# Interactive (fsi) or Visual Studio and run the examples.

Declaring Variables
The most ubiquitous, familiar keyword in F# is the  keyword, which allows programmers to declare functions and variables in their applications.

For example:

This declares a variable called  and assigns it the value. Naturally, we can write the following:

now holds the value 15.

A complete program looks like this:

The statement  prints text out to the console window. As you might have guessed, the code above prints out the values of,  , and. This program results in the following:

"Note to F# Interactive users: all statements in F# Interactive are terminated by (two semicolons). To run the program above in fsi, copy and paste the text above into the fsi window, type, then hit enter."

Values, Not Variables
In F#, "variable" is a misnomer. In reality, all "variables" in F# are immutable; in other words, once you bind a "variable" to a value, it's stuck with that value forever. For that reason, most F# programmers prefer to use "value" rather than "variable" to describe,  , and   above. Behind the scenes, F# actually compiles the "variables" above as static read-only properties.

Declaring Functions
There is little distinction between functions and values in F#. You use the same syntax to write a function as you use to declare a value:

is the name of the function, and it takes two parameters,  and. Notice that each distinct argument in the functional declaration is separated by a space. Similarly, when you execute this function, successive arguments are separated by a space:

This assigns  the return value of this function, which in this case happens to be.

Naturally, we can pass the return value of functions directly into other functions, for example:

This program outputs: num1: 5 num2: 17 num3: 12

Notice that I have to surround the calls to  and   functions with parentheses; this tells F# to treat the value in parentheses as a single argument.

Otherwise, if we wrote, its not only incredibly difficult to read, but it actually passes 7 parameters to the function, which is obviously incorrect.

Function Return Values
Unlike many other languages, F# functions do not have an explicit keyword to return a value. Instead, the return value of a function is simply the value of the last statement executed in the function. For example:

This function takes an integer parameter and returns a string. As you can imagine, the F# function above is equivalent to the following C# code:

Just like C#, F# is a strongly typed language. A function can only return one datatype; for example, the following F# code will not compile:

If you run this code in fsi, you get the following error message: stdin(7,10): error FS0001: This expression was expected to have type   string    but here has type    int

The error message is quite explicit: F# has determined that this function returns a, but the last line of the function returns an  , which is an error.

Interestingly, every function in F# has a return value; of course, programmers don't always write functions that return useful values. F# has a special datatype called, which has just one possible value:. Functions return  when they don't need to return any value to the programmer. For example, a function that prints a string to the console obviously doesn't have a return value:

This function takes  parameter and returns. You can think of  as the equivalent to   in C-style languages.

How to Read Arrow Notation
All functions and values in F# have a data type. Open F# Interactive and type the following:

F# reports the data type using chained arrow notation as follows:

Data types are read from left to right. Before muddying the waters with a more accurate description of how F# functions are built, consider the basic concept of Arrow Notation: starting from the left, our function takes two  inputs and returns a. A function only has one return type, which is represented by the rightmost data type in chained arrow notation.

We can read the following data types as follows:


 * takes one  input, returns a


 * takes two  inputs, returns another


 * takes an  and a   input, returns a

This description is a good introductory way to understand Arrow Notation for a beginner—and if you are new to F# feel free to stop here until you get your feet wet. For those who feel comfortable with this concept as described, the actual way in which F# is implementing these calls is via currying the function.

Partial Function Application
While the above description of Arrow Notation is intuitive, it is not entirely accurate due to the fact that F# implicitly curries functions. This means that a function only ever has a single argument and a single return type, quite at odds with the previous description of Arrow Notation above where in the second and third example two arguments are passed to a function. In reality, a function in F# only ever has a single argument and a single return type. How can this be? Consider this type:

since a function of this type is implicitly curried by F#, there is a two step process to resolve the function when called with two arguments

  a function is called with the first argument that returns a function that takes a float and returns a float. To help clarify currying, lets call this function funX (note that this naming is just for illustration purposes—the function that gets created by the runtime is anonymous).   the second function ('funX' from step 1 above) is called with the second argument, returning a float  

So, if you provide two floats, the result appears as if the function takes two arguments, though this is not actually how the runtime behaves. The concept of currying will probably strike a developer not steeped in functional concepts as very strange and non-intuitive—even needlessly redundant and inefficient, so before attempting a further explanation, consider the benefits of curried functions via an example:

this type has the signature of

then this function:

with the type signature of. Note that the body of  calls   with only one argument—not two. It returns a function that takes an int and returns an int. In other words,  partially applies the    function.

This partial application of a function with multiple argument exemplifies the power of curried functions. It allows deferred application of the function, allowing for more modular development and code re-use—we can re-use the  function to create a new function via partial application. From this, you can glean the power of function currying: it is always breaking down function application to the smallest possible elements, facilitating greater chances for code-reuse and modularity.

Take another example, illustrating the use of partially applied functions as a bookkeeping technique. Note the type signature of  is a function (int -> int) since it is the partial application of

Here we define a new function  on the fly just to keep track of the first value to add. Then later we apply this new 'temp' function  with another value which returns an int. Partially applied functions—enabled by currying—is a very powerful means of controlling complexity in F#. In short, the reason for the indirection resulting from currying function calls affords partial function application and all the benefits it supplies. In other words, the goal of partial function application is enabled by implicit currying.

So while the Arrow Notation is a good shorthand for understanding the type signature of a function, it does so at the price of oversimplification, for a function with the type signature of

is actually (when taking into consideration the implicit currying):

In other words, f is a function that takes an int and returns a function that takes an int and returns an int. Moreover,

is a simplified shorthand for

or, in very difficult to decode English: f is a function that takes an int and returns a function that takes an int and returns a function that takes an int and returns an int. Yikes!

Nested Functions
F# allows programmers to nest functions inside other functions. Nested functions have a number of applications, such as hiding the complexity of inner loops:

The outer function  makes a call to the inner function. Programmers can have an arbitrary level of nested functions as need requires.

Generic Functions
In programming, a generic function is a function that returns an indeterminate type  without sacrificing type safety. A generic type is different from a concrete type such as an  or a  ; a generic type represents a type to be specified later. Generic functions are useful because they can be generalized over many different types.

Let's examine the following function:

F# derives type information of variables from the way variables are used in an application, but F# can't constrain the value  to any particular concrete type, so F# generalizes   to the generic type  :


 * this function takes a generic type  and returns an.

When you call a generic function, the compiler substitutes a function's generic types with the data types of the values passed to the function. As a demonstration, let's use the following function: Which has the type, meaning that the function takes a generic   and a generic   and returns a.

Here are some sample inputs and outputs in F# interactive: calls the function with an  and a , which substitutes   for   and   for. This changes the data type of  to.

calls the function with a  and a , so the function's data type changes to.

just happens to call the function with two s, so the function's data type is incidentally.

Generic functions are strongly typed. For example: The generic function  is defined again, then the   function is defined and it has the type , meaning that this function must be called with two   parameters.

Then  is called, as a parameter to , with two parameters on itself, the first one of type string and the second of type int. This call to  ends up having the type. Since this function has the return type, the code works as expected:

However, we get an error when we reverse the order of the parameters to : The error message is very explicit: The  function takes two   parameters, but   has the return type , so we have a type mismatch.

Later chapters will demonstrate how to use generics in creative and interesting ways.