You are previewing Professional F# 2.0.
O'Reilly logo
Professional F# 2.0

Book Description

This is a book on the F# programming language.

On the surface of things, that is an intuitively obvious statement, given the title of this book. However, despite the apparent redundancy in saying it aloud, the sentence above elegantly describes what this book is about: The authors are not attempting to teach developers how to accomplish tasks from other languages in this one, nor are they attempting to evangelize the language or its feature set or its use "over" other languages. They assume that you are considering this book because you have an interest in learning the F# language: its syntax, its semantics, its pros and cons, and its use in concert with other parts of the .NET ecosystem.

The intended reader is a .NET developer, familiar with at least one of the programming languages in the .NET ecosystem. That language might be C# or Visual Basic, or perhaps C++/CLI, IronPython or IronRuby.

Table of Contents

  1. Copyright
  2. CREDITS
  3. ABOUT THE AUTHORS
  4. ACKNOWLEDGMENTS
  5. FOREWORD
  6. INTRODUCTION
    1. WHO THIS BOOK IS FOR
    2. WHAT THIS BOOK COVERS
    3. HOW THIS BOOK IS STRUCTURED
    4. WHAT YOU NEED TO USE THIS BOOK
    5. CONVENTIONS
    6. SOURCE CODE
    7. ERRATA
    8. P2P.WROX.COM
  7. I. Beginnings
    1. 1. Primer
      1. 1.1. SETUP
      2. 1.2. IT'S THAT TIME OF YEAR AGAIN...
      3. 1.3. STRATEGY
      4. 1.4. THE DELEGATE STRATEGY
      5. 1.5. LAMBDA CALCULUS (BRIEFLY)
      6. 1.6. TYPE INFERENCE
      7. 1.7. IMMUTABILITY
      8. 1.8. EXPRESSIONS, NOT STATEMENTS
      9. 1.9. SUMMARY
  8. II. Basics
    1. 2. Lexical Structure
      1. 2.1. COMMENTS
      2. 2.2. IDENTIFIERS
      3. 2.3. PREPROCESSOR DIRECTIVES
      4. 2.4. SIGNIFICANT WHITESPACE
      5. 2.5. SUMMARY
    2. 3. Primitive Types
      1. 3.1. BOOLEAN
      2. 3.2. NUMERIC TYPES
      3. 3.3. BITWISE OPERATIONS
      4. 3.4. FLOATING-POINT TYPES
      5. 3.5. ARITHMETIC CONVERSIONS
      6. 3.6. STRING AND CHARACTER TYPES
      7. 3.7. UNIT
      8. 3.8. UNITS OF MEASURE TYPES
      9. 3.9. LITERAL VALUES
      10. 3.10. SUMMARY
    3. 4. Control Flow
      1. 4.1. BASIC DECISIONS: IF
      2. 4.2. LOOPING: WHILE/DO
      3. 4.3. LOOPING: FOR
      4. 4.4. EXCEPTIONS
        1. 4.4.1. try...with
        2. 4.4.2. try...finally
        3. 4.4.3. Raising and Throwing Exceptions
        4. 4.4.4. Defining New Exception Types
      5. 4.5. SUMMARY
    4. 5. Composite Types
      1. 5.1. OPTION TYPES
        1. 5.1.1. Option Functions
      2. 5.2. TUPLES
      3. 5.3. ARRAYS
        1. 5.3.1. Array Construction
        2. 5.3.2. Array Access
        3. 5.3.3. Array Functions
          1. 5.3.3.1. Array Meta Functions
          2. 5.3.3.2. Array Application Operations
      4. 5.4. LISTS
        1. 5.4.1. List Construction
        2. 5.4.2. List Access
        3. 5.4.3. List Methods
          1. 5.4.3.1. List Meta Functions
          2. 5.4.3.2. List Application Operations
      5. 5.5. USING LISTS AND ARRAYS
      6. 5.6. SEQUENCES
        1. 5.6.1.
          1. 5.6.1.1. Seq "Meta" Functions
          2. 5.6.1.2. Seq "Application" Operations
      7. 5.7. MAPS
        1. 5.7.1. Map Construction
        2. 5.7.2. Map Access
        3. 5.7.3. Map Functions
      8. 5.8. SETS
      9. 5.9. SUMMARY
    5. 6. Pattern Matching
      1. 6.1. BASICS
      2. 6.2. PATTERN TYPES
        1. 6.2.1. Constant Patterns
        2. 6.2.2. Variable-Binding ("Named") Patterns
        3. 6.2.3. AND, OR Patterns
        4. 6.2.4. Literal Patterns
        5. 6.2.5. Tuple Patterns
        6. 6.2.6. as Patterns
        7. 6.2.7. List patterns
        8. 6.2.8. Array Patterns
        9. 6.2.9. Discriminated Union Patterns
        10. 6.2.10. Record Patterns
      3. 6.3. PATTERN GUARDS
      4. 6.4. ACTIVE PATTERNS
        1. 6.4.1. Single Case
        2. 6.4.2. Partial Case
        3. 6.4.3. Multi-Case
      5. 6.5. SUMMARY
  9. III. Objects
    1. 7. Complex Composite Types
      1. 7.1. TYPE ABBREVIATIONS
      2. 7.2. ENUM TYPES
      3. 7.3. DISCRIMINATED UNION TYPES
      4. 7.4. STRUCTS
        1. 7.4.1. Value Type Implicit Members
        2. 7.4.2. Structs and Pattern-Matching
      5. 7.5. RECORD TYPES
        1. 7.5.1. Record Type Implicit Members
      6. 7.6. SUMMARY
    2. 8. Classes
      1. 8.1. BASICS
        1. 8.1.1. Fields
        2. 8.1.2. Constructors
        3. 8.1.3. Creating
      2. 8.2. MEMBERS
        1. 8.2.1. Properties
          1. 8.2.1.1. Named Property Initialization
          2. 8.2.1.2. Indexer Properties
        2. 8.2.2. Methods
          1. 8.2.2.1. Overloaded Methods
          2. 8.2.2.2. Named Parameters
          3. 8.2.2.3. Optional Parameters
      3. 8.3. STATIC MEMBERS
        1. 8.3.1. Operator Overloading
      4. 8.4. DELEGATES AND EVENTS
        1. 8.4.1. Subscribing
        2. 8.4.2. Delegates
        3. 8.4.3. DelegateEvents
        4. 8.4.4. Beyond DelegateEvents: Events
      5. 8.5. ACCESS MODIFIERS
      6. 8.6. TYPE EXTENSIONS
      7. 8.7. SUMMARY
    3. 9. Inheritance
      1. 9.1. BASICS
        1. 9.1.1. Fields and Constructors
      2. 9.2. OVERRIDING
        1. 9.2.1. Abstract Members
        2. 9.2.2. Default
      3. 9.3. CASTING
        1. 9.3.1. Upcasting
        2. 9.3.2. Downcasting
        3. 9.3.3. Flexible Types
        4. 9.3.4. Boxing and Unboxing
          1. 9.3.4.1. Equality, Hashing, and Comparison
      4. 9.4. INTERFACES
        1. 9.4.1. Implementation
          1. 9.4.1.1. Calling Interface Methods
        2. 9.4.2. Definition
      5. 9.5. OBJECT EXPRESSIONS
      6. 9.6. SUMMARY
    4. 10. Generics
      1. 10.1. BASICS
        1. 10.1.1. Type Parameters
          1. 10.1.1.1. Member Type Parameters
      2. 10.2. TYPE CONSTRAINTS
        1. 10.2.1. Type Constraint
        2. 10.2.2. Equality Constraint
        3. 10.2.3. Comparison Constraint
        4. 10.2.4. Null Constraint
        5. 10.2.5. Constructor Constraint
        6. 10.2.6. Value Type and Reference Type Constraints
        7. 10.2.7. Other Constraints
      3. 10.3. STATICALLY RESOLVED TYPE PARAMETERS
        1. 10.3.1. Explicit Member Constraint
      4. 10.4. SUMMARY
    5. 11. Packaging
      1. 11.1. NAMESPACES
        1. 11.1.1. Referencing a Namespace
        2. 11.1.2. Defining a Namespace
      2. 11.2. MODULES
        1. 11.2.1. Referencing a Module
        2. 11.2.2. Defining a Module
      3. 11.3. SUMMARY
    6. 12. Custom Attributes
      1. 12.1. USING CUSTOM ATTRIBUTES
        1. 12.1.1. EntryPoint
        2. 12.1.2. Obsolete
        3. 12.1.3. Conditional
        4. 12.1.4. ParamArray
        5. 12.1.5. Struct, Class, AbstractClass, Interface, Literal, and Measure
        6. 12.1.6. Assembly Attributes
        7. 12.1.7. DefaultMember
        8. 12.1.8. Serializable, NonSerialized
        9. 12.1.9. AutoOpen
        10. 12.1.10. Other Attributes
      2. 12.2. CREATION AND CONSUMPTION
        1. 12.2.1. Creation
        2. 12.2.2. Consumption
      3. 12.3. SUMMARY
  10. IV. Functional Programming
    1. 13. Functions
      1. 13.1. TRADITIONAL FUNCTION CALLS
      2. 13.2. MATHEMATICAL FUNCTIONS
      3. 13.3. COMING FROM C#
      4. 13.4. FUNCTION ARGUMENTS AND RETURN VALUES
        1. 13.4.1. Automatic Generalization and Restriction
        2. 13.4.2. The inline Keyword
        3. 13.4.3. Type Annotations
        4. 13.4.4. Generics and Type Constraints
        5. 13.4.5. Statically Resolved Type Parameters
      5. 13.5. PARTIAL APPLICATION
        1. 13.5.1. Currying
        2. 13.5.2. Restrictions on Functions and Methods
      6. 13.6. FUNCTIONS AS FIRST CLASS
        1. 13.6.1. Recursive Functions
        2. 13.6.2. Higher Order Functions
        3. 13.6.3. Storing Functions
        4. 13.6.4. Creating Functions at Runtime
          1. 13.6.4.1. Closures
          2. 13.6.4.2. Lambda Expressions
          3. 13.6.4.3. Composition with Partial Application
      7. 13.7. SUMMARY
    2. 14. Immutable Data
      1. 14.1. THE PROBLEM WITH STATE
      2. 14.2. STATE SAFETY
        1. 14.2.1. Programwide State Safety
        2. 14.2.2. Local Data State Safety
      3. 14.3. DATA MUTATION
        1. 14.3.1. Avoiding Mutation
        2. 14.3.2. Bubble and Assign
        3. 14.3.3. Reference Cells
        4. 14.3.4. Passing by Reference
        5. 14.3.5. Message Passing
      4. 14.4. PERFORMANCE CONSIDERATIONS
        1. 14.4.1. Lists
        2. 14.4.2. Arrays
        3. 14.4.3. Sequences
        4. 14.4.4. Tuples
        5. 14.4.5. Records
        6. 14.4.6. structs
      5. 14.5. SUMMARY
    3. 15. Data Types
      1. 15.1. AMBIGUOUSLY TYPED DATA
      2. 15.2. FAILING FAST
      3. 15.3. SPECIFICITY
        1. 15.3.1. Option as an Example
        2. 15.3.2. Encapsulating State in Types
        3. 15.3.3. Avoiding Exceptions
        4. 15.3.4. Data and State Flow
        5. 15.3.5. Recursively Defined Data Types
      4. 15.4. SUMMARY
    4. 16. List Processing
      1. 16.1. COLLECTION ABSTRACTIONS
      2. 16.2. MODULE FUNCTIONS
      3. 16.3. COLLECTION SUBSETS
        1. 16.3.1. filter
        2. 16.3.2. partition
      4. 16.4. ELEMENT TRANSFORMATIONS
        1. 16.4.1. map
        2. 16.4.2. choose
        3. 16.4.3. collect
      5. 16.5. ACCUMULATORS
        1. 16.5.1. reduce
        2. 16.5.2. fold
        3. 16.5.3. scan
      6. 16.6. SUMMARY
    5. 17. Pipelining and Composition
      1. 17.1. BASIC COMPOSITION AND PIPELINING
        1. 17.1.1. Pipelining
          1. 17.1.1.1. Forward Pipe (|>)
          2. 17.1.1.2. Backward Pipe (<|)
        2. 17.1.2. Composition
          1. 17.1.2.1. Forward Composition (>>)
          2. 17.1.2.2. Backward Composition (<<)
      2. 17.2. APPLYING PIPELINING AND COMPOSITION
        1. 17.2.1. From Loops to Pipelining
        2. 17.2.2. From Pipelining to Composition
        3. 17.2.3. Advanced Composition
      3. 17.3. SUMMARY
  11. V. Applications
    1. 18. C#
      1. 18.1. OVERVIEW
      2. 18.2. CALLING C# LIBRARIES FROM F#
        1. 18.2.1. Simple Method Calling Scenarios
        2. 18.2.2. C# Object Construction
        3. 18.2.3. F#, C#, and null
        4. 18.2.4. F# and C# Methods that Expect Delegates
        5. 18.2.5. F# and C# Events
        6. 18.2.6. F# to C# Summary
      3. 18.3. CALLING F# LIBRARIES FROM C#
        1. 18.3.1. Basics of Calling F#
        2. 18.3.2. F# Tuples in C# Programs
        3. 18.3.3. Dealing with F# Records from C#
        4. 18.3.4. Passing Functions to F# Functions
        5. 18.3.5. Dealing with F# Discriminated Unions from C#
        6. 18.3.6. Working with F# Option Types from C#
      4. 18.4. RULES OF THUMB FOR WRITING F# APIS
      5. 18.5. SUMMARY
    2. 19. Databases
      1. 19.1. OVERVIEW
      2. 19.2. RETRIEVING DATA USING ADO.NET
        1. 19.2.1. Creating a Database Connection
        2. 19.2.2. Reading Data
        3. 19.2.3. Filtering Data
        4. 19.2.4. Insert, Update, and Delete
      3. 19.3. F# AND OBJECT RELATIONAL MAPPING
      4. 19.4. INTRODUCING F# ACTIVE RECORD (FAR)
        1. 19.4.1. Reading Data
        2. 19.4.2. Querying Data
        3. 19.4.3. Adding Data
        4. 19.4.4. Updating Data
        5. 19.4.5. Deleting Data
        6. 19.4.6. What Isn't Supported
        7. 19.4.7. Coming Soon
      5. 19.5. HOW FAR WORKS
        1. 19.5.1. Dependencies
        2. 19.5.2. Utility Routines
        3. 19.5.3. Table Creation
        4. 19.5.4. Query Processing
          1. 19.5.4.1. Generating the Parse Tree
        5. 19.5.5. Implementation of Other FOR Operations
          1. 19.5.5.1. The Use Case for F# Active Record
      6. 19.6. SUMMARY
    3. 20. XML
      1. 20.1. OVERVIEW
      2. 20.2. F# AND LINQ-TO-XML
        1. 20.2.1. Reading
        2. 20.2.2. Querying
        3. 20.2.3. Processing
        4. 20.2.4. Writing
        5. 20.2.5. Writing XML to Memory or Other Stream-Based Resources
      3. 20.3. F# AND XML DOM
        1. 20.3.1. Reading
        2. 20.3.2. Querying
        3. 20.3.3. Processing
        4. 20.3.4. Writing
      4. 20.4. F#, XML, AND ACTIVE PATTERNS
        1. 20.4.1. Multi-case Active Patterns
        2. 20.4.2. Partial-Case Active Patterns
      5. 20.5. SUMMARY
    4. 21. ASP.NET MVC
      1. 21.1. OVERVIEW
      2. 21.2. FORECAST'R — THE WORLD'S SIMPLEST WEATHER FORECAST SITE
        1. 21.2.1. Modeling the Domain
        2. 21.2.2. Creating a Repository
          1. 21.2.2.1. Retrieval of "Where On Earth ID"s
          2. 21.2.2.2. From "Where On Earth ID"s to Weather Content
          3. 21.2.2.3. Digging Out the Content
          4. 21.2.2.4. Returning the Weather Result
        3. 21.2.3. Creating the Controller
          1. 21.2.3.1. Setting Up the Controller
          2. 21.2.3.2. The Index Action
          3. 21.2.3.3. The DoQuery Action
          4. 21.2.3.4. The For Action
        4. 21.2.4. Creating Some View Helpers
        5. 21.2.5. Creating the View
      3. 21.3. SUMMARY
    5. 22. Silverlight
      1. 22.1. OVERVIEW
        1. 22.1.1. Software Runtime and Developer Requirements
      2. 22.2. VISUAL STUDIO PROJECT TEMPLATES
        1. 22.2.1. The Silverlight Application
          1. 22.2.1.1. Creating the Silverlight Application
      3. 22.3. XAP
        1. 22.3.1.
          1. 22.3.1.1. Testing Silverlight in the Browser
      4. 22.4. XAML
        1. 22.4.1. The F# Silverlight Library
          1. 22.4.1.1. Unit Testing F# Silverlight Library
      5. 22.5. THE SILVERLIGHT TOOLKIT
        1. 22.5.1. Line Charts and Area Charts
        2. 22.5.2. Designer Tools
      6. 22.6. DATA BINDING
        1. 22.6.1. Design Time Data Binding
          1. 22.6.1.1. Adding a Resource to the Application XAML Element
        2. 22.6.2. Programmatic Data Binding
      7. 22.7. CALCULATING MOVING AVERAGE
      8. 22.8. PUTTING IT ALL TOGETHER
      9. 22.9. SUMMARY
    6. 23. Services
      1. 23.1. OVERVIEW
      2. 23.2. AN F#-BASED WEATHER SERVICE
        1. 23.2.1. The Service Contract
      3. 23.3. LEVERAGING THE DOMAIN MODEL
      4. 23.4. WRITING THE SERVICE CONTROLLER
        1. 23.4.1. Rendering Weather
        2. 23.4.2. Helping the Service Controller
        3. 23.4.3. Service Controller Implementation
        4. 23.4.4. Service Implementation
        5. 23.4.5. Implementing the Service Host
      5. 23.5. CONSUMING SERVICES
        1. 23.5.1. Generating a Service Stub
        2. 23.5.2. Writing the Service Consumer
      6. 23.6. SUMMARY