Cover by Chris Smith

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

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#

Signature

Name

Description

Example

unit

Unit

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

Unit

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

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required