You are previewing Programming F#.

Programming F#

Cover of Programming F# by Chris Smith Published by O'Reilly Media, Inc.

Core Types

Earlier we covered the primitive types available on the .NET platform, but those alone are insufficient for creating meaningful programs. The F# library includes several core types that will allow you to organize, manipulate, and process data. Table 2-9 lists a set of foundational types you will use throughout your F# applications.

Table 2-9. Common types in F#







A unit value


int, float

Concrete type

A concrete type

42, 3.14

'a, 'b

Generic type

A generic (free) type


'a -> 'b

Function type

A function returning a value

fun x -> x + 1

'a * 'b

Tuple type

An ordered collection of values

(1, 2), ("eggs", "ham")

'a list

List type

A list of values

[ 1; 2; 3], [1 .. 3]

'a option

Option type

An optional value

Some(3), None


The unit type is a value signifying nothing of consequence. unit can be thought of as a concrete representation of void and is represented in code via ():

> let x = ();;

val x : unit

> ();;
val it : unit = ()

if expressions without a matching else must return unit because if they did return a value, what would happen if else was hit? Also, in F#, every function must return a value, so if the function doesn’t conceptually return anything—like printf—then it should return a unit value.

The ignore function can swallow a function’s return value if you want to return unit. It is typically used when calling a function for its side effect and you want to ignore its return value:

> let square x = x * x;; val square : int -> int > ignore (square 4);; val ...

The best content for your career. Discover unlimited learning on demand for around $1/day.