You are previewing Programming F#.
O'Reilly logo
Programming F#

Book Description

Why learn F#? This multi-paradigm language not only offers you an enormous productivity boost through functional programming, it also lets you develop applications using your existing object-oriented and imperative programming skills. With Programming F#, you'll quickly discover the many advantages of Microsoft's new language, which includes access to all the great tools and libraries of the .NET platform.

Learn how to reap the benefits of functional programming for your next project -- whether it's quantitative computing, large-scale data exploration, or even a pursuit of your own. With this comprehensive guide, F# team member Chris Smith gives you a head start on the fundamentals and advanced concepts of the F# language.

  • Get a clear understanding of functional programming, and how you can use it to simplify code

  • Gain a solid understanding of the language's core syntax, including object-oriented and imperative styles

  • Simplify concurrent and parallel programming with F# Asynchronous Workflows and the Parallel Extensions to .NET

  • Learn advanced F# concepts, such as quotations and computation expressions

"This book emphasizes simple, clear explanations of the foundational elements of F#, always with an eye on the enjoyment that comes from programming in general, and programming with F# in particular." Don Syme, Principal Researcher and F# Designer, Microsoft Research

Table of Contents

  1. Programming F#
    1. SPECIAL OFFER: Upgrade this ebook with O’Reilly
    2. A Note Regarding Supplemental Files
    3. Foreword
    4. 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, Multiparadigm Programming
        2. Part II, Programming F#
        3. Appendixes
      5. Conventions Used in This Book
      6. How to Contact Us
      7. Using Code Examples
      8. Safari® Books Online
      9. Acknowledgments
    5. I. Multiparadigm Programming
      1. 1. Introduction to F#
        1. Getting to Know F#
        2. Visual Studio 2010
          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. BigInt
          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. Printfn
        5. Anatomy of an F# Program
          1. Modules
            1. Creating modules
            2. Nested modules
          2. Namespaces
          3. Program Startup
      3. 3. Functional Programming
        1. Programming with 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. Anonymous functions
            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
      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. 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
        4. Mutable Collection Types
          1. List<'T>
          2. Dictionary<'K,'V>
          3. HashSet<'T>
        5. Looping Constructs
          1. While Loops
          2. For Loops
            1. Simple for loops
            2. Enumerable for loop
        6. 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. When 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
          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
      7. 7. Applied Functional Programming
        1. Units of Measure
          1. Defining Units of Measure
          2. Converting Between Units of Measure
          3. Generic Units of Measure
        2. Active Patterns
          1. Single-Case Active Patterns
          2. Partial-Case Active Patterns
          3. Parameterized Active Patterns
          4. Multi-Case Active Patterns
          5. Using Active Patterns
            1. Combining active patterns
            2. Nesting active patterns
        3. Using Modules
          1. Converting Modules to Classes
          2. Intentional Shadowing
          3. Controlling Module Usage
        4. Mastering Lists
          1. List Operations
            1. Cons
            2. Append
          2. Using Lists
        5. Tail Recursion
          1. Understanding the Stack
          2. Introducing Tail Recursion
          3. Tail-Recursive Patterns
            1. Accumulator pattern
            2. Continuations
        6. Programming with Functions
          1. Currying
            1. Passing functions as parameters
          2. Eliminating Redundant Code
          3. Closures
        7. Functional Patterns
          1. Memoization
            1. Memoizing recursive functions
          2. Mutable Function Values
          3. Lazy Programming
            1. Reducing memory usage
            2. Abstracting data access
      8. 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
    6. II. Programming F#
      1. 9. 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
      2. 10. 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. 11. 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. Parallel Extensions for .NET
          1. Primitives
            1. Cancellation
            2. Exceptions
          2. Concurrent Data Structures
            1. Concurrent queue
            2. Concurrent dictionary
            3. Concurrent bag
      4. 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
      5. 13. 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
    7. 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 IO
        2. Data Serialization
        3. Deserialization
      4. The F# Libraries
        1. FSharp.Core.dll
        2. The F# PowerPack
          1. SI units of measure
          2. FSLex and FSYacc
    8. B. F# Interop
      1. .NET Interop
        1. C# Interoperating with F#
          1. Discriminated unions
          2. Function values
        2. F# Interoperating with C#
          1. Byref and out parameters
          2. Dealing with null
      2. Unmanaged Interop
        1. Platform Invoke
        2. COM Interop
    9. Index
    10. About the Author
    11. Colophon
    12. SPECIAL OFFER: Upgrade this ebook with O’Reilly