Exercise 1: Types in F#

In this exercise, you will be introduced to the F# interactive console built into Visual Studio 2010. You will also learn how you can leverage F#’s type inference and will also be introduced to the concept of “tuples”.

Task 1 – Observing Type Inference in F#

In this task you will use the F# interactive console to see that F# is capable of inferring data type via the context in which is used.

F# is a statically typed language that makes heavy use of type inference. When provided with a primitive type (int, float, string, etc.) it’s able to infer the type the identifier refers to.

  1. Open the Visual Studio 2010 Command Prompt from Start | All Programs | Microsoft Visual Studio 2010 | Visual Studio Tools | Visual Studio 2010 Command Prompt
  2. Launch F# Interface by launching fsi.exe:

    Figure 1

    The F# interactive console

  3. Type the following command into the F# interactive console:

    F#

    42;;

    Response

    val it : int = 42

    Note:
    The F# console will evaluate the value by determining its type and binding it to an identifier called “it”.

  4. Type the following command in the F# interactive console and press enter:

    F#

    it;;

    Response

    val it : int = 42

    Note:
    In absence of an identifier, the F# environment has bound the value to an identifier called “it”. This identifier is available to other code in the same interactive session.

  5. At the command prompt in the F# interactive session enter the following command and press enter:

    F#

    42.0;;

    Response

    val it : float = 42.0

    Note:
    By adding a decimal to your value F# has inferred that it is float.

  6. At the command prompt in the F# interactive session type the following command and press enter:

    F#

    "42";;

    Response

    val it : string = "42"

    Note:
    By enclosing your value in double quotes F# is able to infer that this value is a string.

Task 2 – Working with Tuples

In this task, you will see how to work with tuples in F#. Tuples are structures in F# that allow developers to ensure that two or more pieces of information are always presented together. This is useful in situations where it may not be sensible to separate data (i.e. a transaction amount, transaction date and transaction ID number).

Note:
In F#, a tuple is simply several values grouped together. Tuples are a fundamental type in F# and can be used anywhere any other value would be used.
  1. At the F# interactive command prompt type the following command and press enter:

    F#

    (42, "Hello F#");;

    Response

    val it : int * string = (42, "Hello F#")

    Note:
    F# has inferred this value as a tuple. In this case, the tuple is composed of an integer value and a string value. The type description shown in the interpreter (“int * string”) indicates that there are two values in this tuple and that the first is an integer and the second is a string.

  2. Tuples are not limited to pairs of values; they can contain as many values as are needed. Type the following command into the F# interactive console and press enter:

    F#

    (42, "Hello F#", 42.0);;

    Response

    val it : int * string * float = (42, "Hello F#", 42.0)

    Note:
    In this case, the tuple that was created is of type “int * string * float” or a structure that takes an integer, a string and a float respectively.

Task 3 – Functions as Values

In this task, you will see that functions in F# are values like any other and can be used in the exact same way.

Note:
In F#, there is no distinction between values and functions because functions themselves are values. They are first-class citizens. As such, a function can be bound to an identifier just like any other value.

  1. Type the command below at the F# interactive command prompt and press enter:

    F#

    fun x -> x + 1;;

    Response

    val it : int -> int = <fun:clo@0>

  2. Since the function that was created in the last step was bound to the “it” identifier, it can be accessed via that identifier. Enter the following command at the F# interactive command prompt:

    F#

    it 41;;

    Response

    val it : int = 42

    Note:
    F# accessed the function that was bound to the “it” identifier and executed the function. But how did F# know this was a function call? Unlike traditional Object-Oriented languages that use parentheses and commas to denote function calls (i.e. “MyFunction (firstParam, secondParam)”), F# knows that the first identifier is a function type (since functions are first-class citizens in F#) and expects all parameters of a function call to be separated by spaces (i.e. “MyFunction firstParam secondParam”).

    Hence, the F# code “it 41” is telling F# to call the function bound to the identifier it and passing it 41 as the first parameter.

  3. As a side effect, the value returned from the function was itself bound to “it”. At the command prompt, type the following command and press enter:

    F#

    it;;

    Response

    val it : int = 42

    Note:
    In this case you can see that the return value of 42 has been bound to the “it” identifier. This means that your function is no longer bound the identifier “it” and therefore cannot be used any longer. Well, that’s just a bit unfortunate. So, in the next section you will learn how to bind values to identifiers that will allow you to access those values as needed.

Next Step

Exercise 2: Using the “Let” Keyword in F#