Skip to main content Accessibility help
×
Hostname: page-component-76fb5796d-25wd4 Total loading time: 0 Render date: 2024-04-25T12:06:27.841Z Has data issue: false hasContentIssue false

3 - Tuples, records and tagged values

Published online by Cambridge University Press:  05 May 2013

Michael R. Hansen
Affiliation:
Technical University of Denmark, Lyngby
Hans Rischel
Affiliation:
Technical University of Denmark, Lyngby
Get access

Summary

Tuples, records and tagged values are compound values obtained by combining values of other types. Tuples are used in expressing “functions of several variables” where the argument is a tuple, and in expressing functions where the result is a tuple. The components in a record are identified by special identifiers called labels. Tagged values are used when we group together values of different kinds to form a single set of values. Tuples, records and tagged values are treated as “first-class citizens” in F#: They can enter into expressions and the value of an expression can be a tuple, a record or a tagged value. Functions on tuples, records or tagged values can be defined by use of patterns.

Tuples

An ordered collection of n values (v1, v2,…,vn), where n > 1, is called an n-tuple. Examples of n-tuples are:

(10, true);;

val it : int * bool = (10, true)

((“abc”,1),–3);;

val it : (string * int) * int = ((“abc”, 1), –3)

A 2-tuple like (10,true) is also called a pair. The last example shows that a pair, for example, ((“abc”,1),–3), can have a component that is again a pair (“abc”,1). In general, tuples can have arbitrary values as components. A 3-tuple is called a triple and a 4-tupleis called a quadruple. An expression like (true) is not a tuple but just the expression true enclosed in brackets, so there is no concept of 1-tuple. The symbol () denotes the only value of type unit (cf. Page 23).

Type
Chapter
Information
Publisher: Cambridge University Press
Print publication year: 2013

Access options

Get access to the full version of this content by using one of the access options below. (Log in options will check for institutional or personal access. Content may require purchase if you do not have access.)

Save book to Kindle

To save this book to your Kindle, first ensure coreplatform@cambridge.org is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.

Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.

Find out more about the Kindle Personal Document Service.

Available formats
×

Save book to Dropbox

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.

Available formats
×

Save book to Google Drive

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.

Available formats
×