Chapter 5. Composite Types


  • Understanding option types

  • Working with tuples

  • Using arrays, lists and sequences

  • Creating and using maps and sets

Like many other .NET languages, F# provides not only a set of primitive types for describing the basic atoms of data (strings, integers, and so on), but also a set of types for gathering those atoms into larger structures. These composite types are also built into the language, and in some cases directly mirror capabilities found within the .NET Base Class Library or the CLR.

In many cases, F# developers will find that these composite types can serve where normally developers in other languages would have to create a complete class type. For example, as with many functional languages, F# developers can find that a collection of functions plus a tuple type instance or list instance can be sufficient to model the problem domain, without having to create a standalone class to represent the data. Much of the F# library is built in precisely this manner, wrapped into a module (see Chapter 11 for lexical scoping).


The simplest composite type to understand is the option type, which is effectively an either/or type similar in some ways to the Boolean data type, but with an entirely different purpose and use.

Options are similar to Booleans in that there are only two acceptable values for a given option, None, indicating an absence of value, and Some, which indicates a value. As can be easily inferred from the description ...

Get Professional F# 2.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.