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
5 - Collections: Lists, maps and sets
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
Functional languages make it easy to express standard recursion patterns in the form of higher-order functions. A collection of such higher-order functions on lists, for example, provides a powerful library where many recursive functions can be obtained directly by application of higher-order library functions. This has two important consequences:
1. The functions in the library correspond to natural abstract concepts and conscious use of them supports high-level program design, and
2. these functions support code reuse because you can make many functions simply by applying library functions.
In this chapter we shall study libraries for lists, sets and maps, which are parts of the collection library of F#. This part of the collection library is studied together since:
It constitutes the immutable part of the collection library. The list, set and map collections are finite collections programmed in a functional style.
There are many similarities in the corresponding library functions.
This chapter is a natural extension of Chapter 4 since many of the patterns introduced in that chapter correspond to higher-order functions for lists and since more natural program designs can be given for the two examples in Section 4.6 using sets and maps.
We will focus on the main concepts and applications in this book, and will deliberately not cover the complete collection library of F#. The functions of the collection library do also apply to (mutable) arrays. We address this part in Section 8.10.
- Type
- Chapter
- Information
- Functional Programming Using F# , pp. 93 - 120Publisher: Cambridge University PressPrint publication year: 2013