Book contents
- Frontmatter
- Contents
- Preface
- 1 Getting started
- 2 Values, operators, expressions and functions
- 3 Tuples, records and tagged values
- 4 Lists
- 5 Collections: Lists, maps and sets
- 6 Finite trees
- 7 Modules
- 8 Imperative features
- 9 Efficiency
- 10 Text processing programs
- 11 Sequences
- 12 Computation expressions
- 13 Asynchronous and parallel computations
- Appendix A Programs from the keyword example
- Appendix B The TextProcessing library
- Appendix C The dialogue program from Chapter 13
- References
- Index
3 - Tuples, records and tagged values
Published online by Cambridge University Press: 05 May 2013
- Frontmatter
- Contents
- Preface
- 1 Getting started
- 2 Values, operators, expressions and functions
- 3 Tuples, records and tagged values
- 4 Lists
- 5 Collections: Lists, maps and sets
- 6 Finite trees
- 7 Modules
- 8 Imperative features
- 9 Efficiency
- 10 Text processing programs
- 11 Sequences
- 12 Computation expressions
- 13 Asynchronous and parallel computations
- Appendix A Programs from the keyword example
- Appendix B The TextProcessing library
- Appendix C The dialogue program from Chapter 13
- References
- Index
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
- Functional Programming Using F# , pp. 43 - 66Publisher: Cambridge University PressPrint publication year: 2013