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.
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).
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.
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.
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.