Programming F# 3.0, 2nd Edition

Book description

Why learn F#? With this guide, you’ll learn how this multi-paradigm language not only offers you an enormous productivity boost through functional programming, but also lets you develop applications using your existing object-oriented and imperative programming skills. You’ll quickly discover the many advantages of the language, including access to all the great tools and libraries of the .NET platform.

Reap the benefits of functional programming for your next project, whether you’re writing concurrent code, or building data- or math-intensive applications. With this comprehensive book, former F# team member Chris Smith gives you a head start on the fundamentals and walks you through advanced concepts of the F# language.

  • Learn F#’s unique characteristics for building applications
  • Gain a solid understanding of F#’s core syntax, including object-oriented and imperative styles
  • Make your object-oriented code better by applying functional programming patterns
  • Use advanced functional techniques, such as tail-recursion and computation expressions
  • Take advantage of multi-core processors with asynchronous workflows and parallel programming
  • Use new type providers for interacting with web services and information-rich environments
  • Learn how well F# works as a scripting language

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Introducing F#
    2. Who This Book Is For
    3. What You Need to Get Going
    4. How the Book Is Organized
      1. Part I
      2. Part II
      3. Part III
    5. Part IV
    6. Conventions Used in This Book
    7. Using Code Examples
    8. Safari® Books Online
    9. I’d Like to Hear from You
    10. Acknowledgments
  2. I. Multiparadigm Programming
    1. 1. Introduction to F#
      1. Getting to Know F#
      2. Visual Studio 11
        1. Your Second F# Program
        2. Values
        3. Whitespace Matters
        4. .NET Interop
        5. Comments
      3. F# Interactive
      4. Managing F# Source Files
    2. 2. Fundamentals
      1. Primitive Types
        1. Numeric Primitives
        2. Arithmetic
        3. Conversion Routines
        4. BigInteger
        5. Bitwise Operations
        6. Characters
        7. Strings
        8. Boolean Values
      2. Comparison and Equality
      3. Functions
        1. Type Inference
        2. Generic Functions
        3. Scope
        4. Control Flow
      4. Core Types
        1. Unit
        2. Tuple
        3. Lists
          1. List ranges
          2. List comprehensions
          3. List module functions
        4. Aggregate Operators
          1. List.map
          2. List.fold
          3. Folding right-to-left
          4. List.iter
        5. Option
        6. Printf
      5. Organizing F# Code
        1. Modules
          1. Creating modules
          2. Nested modules
        2. Namespaces
        3. Program Startup
    3. 3. Functional Programming
      1. Understanding Functions
        1. Immutability
        2. Function Values
          1. Partial function application
          2. Functions returning functions
        3. Recursive Functions
          1. Mutual recursion
        4. Symbolic Operators
        5. Function Composition
          1. Pipe-forward operator
          2. Forward composition operator
          3. Pipe-backward operator
          4. Backward composition operator
      2. Pattern Matching
        1. Match Failure
        2. Named Patterns
        3. Matching Literals
        4. when Guards
        5. Grouping Patterns
        6. Matching the Structure of Data
          1. Tuples
          2. Lists
          3. Options
        7. Outside of Match Expressions
          1. let bindings
          2. Function parameters
          3. Wildcard patterns
        8. Alternate Lambda Syntax
      3. Discriminated Unions
        1. Using Discriminated Unions for Tree Structures
        2. Pattern Matching
        3. Methods and Properties
      4. Records
        1. Cloning Records
        2. Pattern Matching
        3. Type Inference
        4. Methods and Properties
      5. Lazy Evaluation
        1. Lazy Types
      6. Sequences
        1. Sequence Expressions
        2. Seq Module Functions
          1. Seq.take
          2. Seq.unfold
        3. Aggregate Operators
          1. Seq.iter
          2. Seq.map
          3. Seq.fold
      7. Queries
        1. Query Expressions
        2. Query Operators
          1. Selection operators
          2. Sorting operators
    4. 4. Imperative Programming
      1. Understanding Memory in .NET
        1. Value Types Versus Reference Types
        2. Default Values
        3. Reference Type Aliasing
      2. Changing Values
        1. Reference Cells
        2. Mutable Records
      3. Units of Measure
        1. Defining Units of Measure
        2. Converting Between Units of Measure
        3. Generic Units of Measure
      4. Arrays
        1. Indexing an Array
        2. Array Slices
        3. Creating Arrays
        4. Pattern Matching
        5. Array Equality
        6. Array Module Functions
          1. partition
          2. tryFind and tryFindIndex
          3. Aggregate operators
        7. Multidimensional Arrays
          1. Rectangular arrays
          2. Jagged arrays
      5. Mutable Collection Types
        1. List<'T>
        2. Dictionary<'K,'V>
        3. HashSet<'T>
      6. Looping Constructs
        1. While Loops
        2. For Loops
          1. Simple for loops
          2. Enumerable for loop
      7. Exceptions
        1. Handling Exceptions
        2. Reraising Exceptions
        3. Defining Exceptions
    5. 5. Object-Oriented Programming
      1. Programming with Objects
        1. The Benefits of OOP
        2. Where OOP Breaks Down
      2. Understanding System.Object
        1. Common Methods
          1. ToString
          2. GetHashCode
          3. Equals
          4. GetType
        2. Object Equality
        3. Generated Equality
      3. Understanding Classes
        1. Explicit Construction
        2. Implicit Class Construction
        3. Generic Classes
        4. The Self Identifier
      4. Methods and Properties
        1. Properties
          1. Auto-properties
        2. Setting Properties in the Constructor
        3. Methods
        4. Static Methods, Properties, and Fields
          1. Static fields
        5. Method Overloading
        6. Accessibility Modifiers
          1. Accessibility modifiers on module values
          2. F# signature files
      5. Inheritance
        1. Method Overriding
        2. Categories of Classes
          1. Abstract classes
          2. Sealed classes
        3. Casting
          1. Static upcast
          2. Dynamic cast
          3. Pattern matching against types
    6. 6. .NET Programming
      1. The .NET Platform
        1. The CLI
        2. Garbage Collection
      2. Interfaces
        1. Using Interfaces
        2. Defining Interfaces
      3. Object Expressions
        1. Object Expressions for Interfaces
        2. Object Expressions for Derived Classes
      4. Extension Methods
      5. Extending Modules
      6. Enumerations
        1. Creating Enumerations
        2. Conversion
        3. When to Use an Enum Versus a Discriminated Union
      7. Structs
        1. Creating Structs
        2. Restrictions
        3. When to Use a Struct Versus a Record
  3. II. Programming F#
    1. 7. Applied Functional Programming
      1. Active Patterns
        1. Single-Case Active Patterns
        2. Partial Active Patterns
        3. Parameterized Active Patterns
        4. Multicase Active Patterns
        5. Using Active Patterns
          1. Combining active patterns
          2. Nesting active patterns
      2. Using Modules
        1. Converting Modules to Classes
        2. Intentional Shadowing
        3. Controlling Module Usage
      3. Mastering Lists
        1. List Operations
          1. Cons
          2. Append
        2. Using Lists
      4. Tail Recursion
        1. Understanding the Stack
        2. Introducing Tail Recursion
        3. Tail-Recursive Patterns
          1. Accumulator pattern
          2. Continuations
      5. Programming with Functions
        1. Partial Function Application
          1. Passing functions as parameters
        2. Eliminating Redundant Code
        3. Closures
      6. Functional Patterns
        1. Memoization
          1. Memoizing recursive functions
        2. Mutable Function Values
        3. Lazy Programming
          1. Reducing memory usage
          2. Abstracting data access
      7. Functional Data Structures
        1. Functional Set
        2. Functional Map
    2. 8. Applied Object-Oriented Programming
      1. Operators
        1. Operator Overloading
        2. Indexers
        3. Adding Slices
      2. Generic Type Constraints
      3. Delegates and Events
        1. Defining Delegates
        2. Combining Delegates
      4. Events
        1. Creating Events
          1. Delegates for events
          2. Creating and raising the event
          3. Subscribing to events
        2. The Event<_,_> Class
        3. The Observable Module
          1. Observable.add
          2. Observable.merge
          3. Observable.map
        4. Creating .NET Events
    3. 9. Asynchronous and Parallel Programming
      1. Working with Threads
        1. Spawning Threads
          1. Thread.Sleep
          2. Thread.Abort
        2. The .NET Thread Pool
        3. Sharing Data
          1. Race conditions
          2. Deadlocks
      2. Asynchronous Programming
      3. Asynchronous Workflows
        1. The Async Library
          1. Starting async tasks
          2. Exceptions
          3. Cancellation
        2. Async Operations
        3. Custom Async Primitives
        4. Limitations
      4. Parallel Programming
        1. Parallel.For
        2. The Array.Parallel Module
      5. Task Parallel Library
        1. Primitives
          1. Cancellation
          2. Exceptions
        2. Concurrent Data Structures
          1. Concurrent queue
          2. Concurrent dictionary
          3. Concurrent bag
    4. 10. Scripting
      1. F# Script Files
      2. Directives
        1. General Directives
          1. __SOURCE_DIRECTORY__
          2. __SOURCE_FILE__
        2. F# Script-Specific Directives
          1. #r
          2. #I
          3. #load
      3. F# Script Recipes
        1. Colorful Output
        2. Producing Sound
        3. Walking a Directory Structure
        4. Starting Processes Easily
        5. Automating Microsoft Office
    5. 11. Data Processing
      1. Indexing
        1. The Index Data Structure
          1. Token posting lists
        2. MapReduce Processing
          1. Benefits
          2. Cloud hosts
        3. Search Index Mapper
        4. Search Index Reducer
      2. Querying
        1. Lex and Yacc
          1. Parsing
          2. Lexing
        2. Query Processing
          1. Token posting lists
          2. Keyword queries
          3. Exact phrase queries
          4. Logical operators
          5. Putting them together
          6. Next steps
  4. III. Extending the F# Language
    1. 12. Reflection
      1. Attributes
        1. Applying Attributes
          1. Attribute targets
          2. Applying multiple attributes
        2. Defining New Attributes
      2. Type Reflection
        1. Accessing Types
          1. Accessing generic types
          2. Inspecting methods
          3. Inspecting attributes
        2. Reflecting on F# Types
          1. Tuples
          2. Discriminated unions
          3. Records
      3. Dynamic Instantiation
        1. Instantiating Types
        2. Instantiating F# Types
        3. Dynamic Invocation
        4. The Question Mark Operators
      4. Using Reflection
        1. Declarative Programming
        2. Plug-in Architecture
          1. Plug-in interfaces
          2. Loading assemblies
          3. Loading plug-ins
    2. 13. Computation Expressions
      1. Toward Computation Expressions
      2. Computation Expression Builders
      3. Custom Computation Expression Builders
        1. Asynchronous Workflows
        2. The Rounding Workflow
        3. The State Workflow
          1. Building up the computation
          2. Implementation
    3. 14. Quotations
      1. Quotation Basics
        1. Decomposing Quotations
          1. Literal values
          2. Function calls
          3. Function values
        2. Quoting Method Bodies
        3. Decomposing Arbitrary Code
        4. Application: Deferring Computation to Other Platforms
      2. Generating Quotation Expressions
        1. Expression Holes
        2. Evaluating Quotations
        3. Application: Generating Derivatives
    4. 15. Type Providers
      1. Typed Data Versus Typed Languages
        1. Wrapper Assemblies
        2. F# Type Providers
          1. Advantage over shim libraries
          2. Advantage over IDEs
      2. Type Providers
        1. SQL Data Type Providers
          1. LINQ to SQL
          2. DbmlFile
        2. Entity Framework
        3. Web Service Type Providers
          1. The WsdlService Provider
          2. The ODataService Provider
        4. Custom Type Providers
  5. IV. Appendixes
    1. A. Overview of .NET Libraries
      1. Visualization
        1. Windows Forms
        2. Windows Presentation Foundation
          1. XAML, the eXtensible Application Markup Language
          2. Binding
      2. Data Processing
        1. Regular Expressions
          1. Metacharacters
          2. String manipulation
          3. Capture groups
        2. Working with XML
          1. Writing XML
          2. Reading XML
      3. Storing Data
        1. File I/O
        2. Data Serialization
        3. Deserialization
    2. B. F# Interop
      1. .NET Interop
        1. Nullable Types
          1. Linq.NullableOperators
        2. C# Interoperating with F#
          1. Discriminated unions
          2. Function values
        3. F# Interoperating with C#
          1. Byref and out parameters
          2. Dealing with null
      2. Unmanaged Interop
        1. Platform Invoke
        2. COM Interop
  6. Index
  7. About the Author
  8. Colophon
  9. Copyright

Product information

  • Title: Programming F# 3.0, 2nd Edition
  • Author(s): Chris Smith
  • Release date: October 2012
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449320294