E-Book, Englisch, 624 Seiten
Syme / Granicz / Cisternino Expert F# 2.0
1. ed
ISBN: 978-1-4302-2432-7
Verlag: Apress
Format: PDF
Kopierschutz: 1 - PDF Watermark
E-Book, Englisch, 624 Seiten
ISBN: 978-1-4302-2432-7
Verlag: Apress
Format: PDF
Kopierschutz: 1 - PDF Watermark
Expert F# 2.0 is about practical programming in a beautiful language that puts the power and elegance of functional programming into the hands of professional developers. In combination with .NET, F# achieves unrivaled levels of programmer productivity and program clarity. Expert F# 2.0 is The authoritative guide to F# by the inventor of F# A comprehensive reference of F# concepts, syntax, and features A treasury of expert F# techniques for practical, real-world programming F# isn't just another functional programming language. It's a general-purpose language ideal for real-world development. F# seamlessly integrates functional, imperative, and object-oriented programming styles so you can flexibly and elegantly solve any programming problem. Whatever your background, you'll find that F# is easy to learn, fun to use, and extraordinarily powerful. F# will change the way you think about-and go about-programming. Written by F#'s inventor and two major contributors to its development, Expert F# 2.0 is the authoritative, comprehensive, and in-depth guide to the language and its use. Designed to help others become experts, the first part of the book quickly yet carefully describes the F# language. The second part then shows how to use F# elegantly for a wide variety of practical programming tasks. The world's foremost experts in F# show you how to program in F# the way they do!
strongDon Syme/strong is a principal researcher at Microsoft Research, and the main designer of F#. Since joining Microsoft Research in 1998, he has been a seminal contributor to a wide variety of leading-edge projects, including generics in C# and the .NET Common Language Runtime, F# itself, F# asynchronous programming and units of measure in F#. He received a Ph.D. from the University of Cambridge Computer Laboratory in 1999.
Autoren/Hrsg.
Weitere Infos & Material
1;Copyright Page;3
2;Contents at a Glance;5
3;Table of Contents;7
4;Foreword;22
5;About the Authors;24
6;About the Technical Reviewer;25
7;Acknowledgments;26
8;CHAPTER 1 Introduction;27
8.1;The Genesis of F#;27
8.2;About This Book;28
8.3;Who This Book Is For;31
9;CHAPTER 2 Getting Started with F# and .NET;32
9.1;Creating Your First F# Program;32
9.1.1;Documenting Code Using XMLDocs;34
9.1.2;Using let;34
9.1.3;Understanding Types;35
9.1.4;Calling Functions;36
9.1.5;Lightweight Syntax;37
9.1.6;Understanding Scope;38
9.1.7;Using Data Structures;39
9.1.8;Using Properties and the Dot-Notation;40
9.1.9;Using Tuples;41
9.1.10;Using Imperative Code;43
9.2;Using .NET Libraries from F#;44
9.2.1;Using open to Access Namespaces and Modules;45
9.2.2;Using new and Setting Properties;46
9.2.3;Fetching a Web Page;47
9.3;Summary;49
10;CHAPTER 3 Creating Your First F# Program—Introducing FunctionalProgramming;50
10.1;Getting Started with F# Arithmetic;50
10.1.1;Basic Literals;50
10.1.2;Arithmetic Operators;51
10.1.3;Bitwise Operations;52
10.1.4;Arithmetic Conversions;53
10.1.5;Arithmetic Comparisons;54
10.1.6;Overloaded Math Functions;54
10.2;Introducing Simple Strings;55
10.2.1;Working with String Literals and Primitives;55
10.2.2;Building Strings;57
10.3;Working with Lists and Options;58
10.3.1;Using F# Lists;58
10.3.2;Using F# Option Values;62
10.3.3;Using Option Values for Control;63
10.4;Working with Conditionals: && and ||;63
10.5;Defining Recursive Functions;64
10.6;Introducing Function Values;65
10.6.1;Using Anonymous Function Values;66
10.6.2;Computing with Aggregate Operators;67
10.6.3;Composing Functions with >>;69
10.6.4;Building Functions with Partial Application;70
10.6.5;Using Local Functions;71
10.6.6;Using Functions as Abstract Values;72
10.6.7;Iterating with Aggregate Operators;73
10.6.8;Abstracting Control with Functions;73
10.6.9;Using .NET Methods as First-Class Functions;74
10.7;Getting Started with Pattern Matching;75
10.7.1;Matching on Structured Values;76
10.7.2;Guarding Rules and Combining Patterns;78
10.8;Getting Started with Sequences;78
10.8.1;Using Range Expressions;79
10.8.2;Iterating a Sequence;80
10.8.3;Transforming Sequences with Aggregate Operators;80
10.8.4;Which Types Can Be Used as Sequences?;81
10.8.5;Using Lazy Sequences from External Sources;82
10.9;Using Sequence Expressions;83
10.9.1;Creating Sequence Expressions Using for;83
10.9.2;Enriching Sequence Expressions with Additional Logic;84
10.9.3;Generating Lists and Arrays Using Sequence Expressions;85
10.10;Exploring Some Simple Type Definitions;85
10.10.1;Defining Type Abbreviations;85
10.10.2;Defining Records;86
10.10.3;Handling Non-Unique Record Field Names;87
10.10.4;Cloning Records;88
10.10.5;Defining Discriminated Unions;88
10.10.6;Using Discriminated Unions as Records;90
10.10.7;Defining Multiple Types Simultaneously;91
10.11;Summary;91
11;CHAPTER 4 Introducing ImperativeProgramming;92
11.1;Imperative Looping and Iterating;93
11.1.1;Simple for Loops;93
11.1.2;Simple while Loops;93
11.1.3;More Iteration Loops over Sequences;94
11.2;Using Mutable Records;94
11.2.1;Mutable Reference Cells;95
11.2.2;Avoiding Aliasing;97
11.2.3;Hiding Mutable Data;98
11.3;Using Mutable Locals;99
11.4;Working with Arrays;99
11.4.1;Generating and Slicing Arrays;101
11.4.2;Two-Dimensional Arrays;102
11.5;Introducing the Imperative .NET Collections;102
11.5.1;Using Resizeable Arrays;103
11.5.2;Using Dictionaries;104
11.5.3;Using Dictionary’s TryGetValue;104
11.5.4;Using Dictionaries with Compound Keys;105
11.5.5;Some Other Mutable Data Structures;106
11.6;Exceptions and Controlling Them;106
11.6.1;Catching Exceptions;108
11.6.2;Using try . . . finally;108
11.6.3;Defining New Exception Types;109
11.7;Having an Effect: Basic I/O;110
11.7.1;Very Simple I/O: Reading and Writing Files;110
11.7.2;.NET I/O via Streams;111
11.7.3;Some Other I/O-Related Types;112
11.7.4;Using System.Console;113
11.7.5;Using printf and Friends;113
11.7.6;Generic Structural Formatting;115
11.7.7;Cleaning Up with IDisposable, use, and using;115
11.8;Working with null Values;116
11.9;Some Advice: Functional Programming with Side Effects;117
11.9.1;Consider Replacing Mutable Locals and Loops with Recursion;118
11.9.2;Separate Pure Computation from Side-Effecting Computations;118
11.9.3;Separate Mutable Data Structures;118
11.9.4;Not All Side Effects Are Equal;119
11.9.5;Avoid Combining Imperative Programming and Laziness;120
11.10;Summary;121
12;CHAPTER 5Mastering Types and Generics;122
12.1;Understanding Generic Type Variables;122
12.2;Writing Generic Functions;123
12.3;Understanding Some Important Generic Functions;124
12.3.1;Generic Comparison;124
12.3.2;Generic Hashing;125
12.3.3;Generic Pretty-Printing;126
12.3.4;Generic Boxing and Unboxing;126
12.3.5;Generic Binary Serialization via the .NET Libraries;127
12.4;Making Things Generic;129
12.4.1;Generic Algorithms through Explicit Arguments;129
12.4.2;Generic Algorithms through Abstract Object Types;131
12.4.3;Arithmetic Operators and Generic Algorithms through Inlining;133
12.5;Understanding .NET Types;134
12.5.1;Reference Types and Value Types;134
12.5.2;Other Flavors of .NET Types;135
12.6;Understanding Subtyping;136
12.6.1;Casting Up Statically;136
12.6.2;Casting Down Dynamically;136
12.6.3;Performing Type Tests via Pattern Matching;137
12.6.4;Knowing When Upcasts Are Applied Automatically;138
12.6.5;Flexible Types;139
12.7;Units of Measure;140
12.8;Troubleshooting Type-Inference Problems;142
12.8.1;Using a Visual Editing Environment;142
12.8.2;Using Type Annotations;142
12.8.3;Understanding the Value Restriction;143
12.8.4;Working Around the Value Restriction;144
12.8.5;Understanding Generic Overloaded Operators;146
12.9;Summary;147
13;CHAPTER 6Working with Objects and Modules;148
13.1;Getting Started with Objects and Members;148
13.2;Using Classes;151
13.3;Adding Further Object Notation to Your Types;155
13.3.1;Working with Indexer Properties;155
13.3.2;Adding Overloaded Operators;155
13.3.3;Using Named and Optional Arguments;157
13.3.4;Using Optional Property Settings;158
13.3.5;Adding Method Overloading;159
13.4;Defining Object Types with Mutable State;160
13.5;Getting Started with Object Interface Types;162
13.5.1;Defining New Object Interface Types;163
13.5.2;Implementing Object Interface Types Using Object Expressions;164
13.5.3;Implementing Object Interface Types Using Concrete Types;165
13.5.4;Using Common Object Interface Types from the .NET Libraries;166
13.5.5;Understanding Hierarchies of Object Interface Types;167
13.6;More Techniques to Implement Objects;168
13.6.1;Combining Object Expressions and Function Parameters;168
13.6.2;Defining Partially Implemented Class Types;169
13.6.3;Using Partially Implemented Types via Delegation;170
13.6.4;Using Partially Implemented Types via Implementation Inheritance;170
13.7;Using Modules and Static Members;172
13.8;Extending Existing Types and Modules;173
13.9;Working with F# Objects and .NET Types;175
13.9.1;Structs;176
13.9.2;Delegates;176
13.9.3;Enums;176
13.10;Summary;177
14;CHAPTER 7 Encapsulating and PackagingYour Code;178
14.1;Hiding Things Away;178
14.1.1;Hiding Things with Local Definitions;179
14.1.2;Hiding Things with Accessibility Annotations;181
14.2;Using Namespaces and Modules;184
14.2.1;Putting Your Code in a Namespace;184
14.2.2;Using Files as Modules;185
14.3;Creating Assemblies, DLLs, and EXEs;186
14.3.1;Compiling EXEs;186
14.3.2;Compiling DLLs;186
14.3.3;Mixing Scripting and Compiled Code;188
14.3.4;Choosing Optimization Settings;189
14.3.5;Generating Documentation;189
14.3.6;Building Shared Libraries and Using the Global Assembly Cache;190
14.3.7;Using Static Linking;191
14.4;Using Signature Types and Files;192
14.4.1;Using Explicit Signature Types and Signature Files;192
14.4.2;When Are Signature Types Checked?;193
14.5;Packaging Applications;194
14.5.1;Packaging Different Kinds of Code;194
14.5.2;Using Data and Configuration Settings;195
14.6;Summary;198
15;CHAPTER 8Mastering F#: Common Techniques;199
15.1;Equality, Hashing, and Comparison;199
15.1.1;Asserting Equality, Hashing, and Comparison Using Attributes;200
15.1.2;Fully Customizing Equality, Hashing, and Comparison on a Type;201
15.1.3;Suppressing Equality, Hashing, and Comparison on a Type;203
15.1.4;Customizing Generic Collection Types;203
15.2;Efficient Precomputation and Caching;204
15.2.1;Precomputation and Partial Application;204
15.2.2;Precomputation and Objects;206
15.2.3;Memoizing Computations;207
15.2.4;Lazy Values;209
15.2.5;Other Variations on Caching and Memoization;210
15.3;Cleaning Up Resources;210
15.3.1;Cleaning Up with use;211
15.3.2;Managing Resources with More Complex Lifetimes;213
15.3.3;Cleaning Up Internal Objects;214
15.3.4;Cleaning Up Unmanaged Objects;215
15.3.5;Cleaning Up in Sequence Expressions;216
15.3.6;Using using;218
15.4;Stack as a Resource: Tail Calls and Recursion;218
15.4.1;Tail Recursion and List Processing;219
15.4.2;Tail Recursion and Object-Oriented Programming;221
15.4.3;Tail Recursion and Processing Unbalanced Trees;222
15.4.4;Using Continuations to Avoid Stack Overflows;223
15.4.5;Another Example: Processing Syntax Trees;225
15.5;Events;226
15.5.1;Events as First-Class Values;227
15.5.2;Creating and Publishing Events;228
15.6;Summary;229
16;CHAPTER 9 Introducing Language-OrientedProgramming;230
16.1;Using XML as a Concrete Language Format;231
16.1.1;Using the System.Xml Namespace;231
16.1.2;From Concrete XML to Abstract Syntax;233
16.2;Working with Abstract Syntax Representations;236
16.2.1;Abstract Syntax Representations: Less Is More;236
16.2.2;Processing Abstract Syntax Representations;236
16.2.3;Transformational Traversals of Abstract Syntax Representations;238
16.2.4;Using On-Demand Computation with Abstract Syntax Trees;238
16.2.5;Caching Properties in Abstract Syntax Trees;240
16.2.6;Memoizing Construction of Syntax Tree Nodes;241
16.3;Introducing Active Patterns;243
16.3.1;Converting the Same Data to Many Views;243
16.3.2;Matching on .NET Object Types;245
16.3.3;Defining Partial and Parameterized Active Patterns;246
16.3.4;Hiding Abstract Syntax Implementations with Active Patterns;246
16.4;Embedded Computational Languages with Workflows;248
16.4.1;An Example: Success/Failure Workflows;250
16.4.2;Defining a Workflow Builder;253
16.4.3;Workflows and Untamed Side Effects;255
16.4.4;Example: Probabilistic Workflows;256
16.4.5;Combining Workflows and Resources;261
16.4.6;Recursive Workflow Expressions;261
16.5;Using F# Reflection;262
16.5.1;Reflecting on Types;262
16.5.2;Schema Compilation by Reflecting on Types;262
16.5.3;Using the F# Dynamic Reflection Operators;266
16.6;Using F# Quotations;267
16.6.1;Example: Using F# Quotations for Error Estimation;268
16.6.2;Resolving Top Definitions;270
16.7;Summary;271
17;CHAPTER 10Using the F# and .NET Libraries;272
17.1;A High-Level Overview;272
17.1.1;Namespaces from the .NET Framework;273
17.1.2;Namespaces from the F# Libraries;275
17.2;Using the System Types;276
17.3;Using Regular Expressions and Formatting;278
17.3.1;Matching with System.Text.RegularExpressions;278
17.3.2;Formatting Strings Using .NET Formatting;283
17.3.3;Encoding and Decoding Unicode Strings;284
17.3.4;Encoding and Decoding Binary Data;285
17.4;Using Further F# and .NET Data Structures;285
17.4.1;System.Collections.Generic and Other .NET Collections;285
17.5;Introducing Microsoft.FSharp.Math;286
17.5.1;Using Matrices and Vectors;286
17.5.2;Using Operator Overloads on Matrices and Vectors;288
17.6;Supervising and Isolating Execution;288
17.7;Further Libraries for Reflective Techniques;289
17.7.1;Using General Types;289
17.7.2;Using Microsoft.FSharp.Reflection;290
17.8;Some Other .NET Types You May Encounter;290
17.9;Some Other .NET Libraries;291
17.10;Summary;292
18;CHAPTER 11Building Graphical User Interfaces;293
18.1;Writing “Hello, World!” in a Click;293
18.2;Understanding the Anatomy of a Graphical Application;294
18.3;Composing User Interfaces;295
18.4;Drawing Applications;300
18.5;Writing Your Own Controls;304
18.5.1;Developing a Custom Control;304
18.5.2;Anatomy of a Control;307
18.6;Displaying Samples from Sensors;308
18.6.1;Building the GraphControl: The Model;310
18.6.2;Building the GraphControl: Style Properties and Controller;312
18.6.3;Building the GraphControl: The View;315
18.6.4;Putting It Together;319
18.7;Creating a Mandelbrot Viewer;320
18.7.1;Computing Mandelbrot;321
18.7.2;Setting Colors;322
18.7.3;Creating the Visualization Application;325
18.7.4;Creating the Application Plumbing;327
18.8;Windows Presentation Foundation;331
18.8.1;When GUIs Meet the Web;333
18.8.2;Drawing;334
18.8.3;Controls;337
18.8.4;Bitmaps and Images;340
18.8.5;Final Considerations;342
18.9;Summary;342
19;CHAPTER 12 Working with SymbolicRepresentations;343
19.1;Symbolic Differentiation and Expression Rendering;344
19.1.1;Modeling Simple Algebraic Expressions;344
19.1.2;Implementing Local Simplifications;346
19.1.3;A Richer Language of Algebraic Expressions;347
19.1.4;Parsing Algebraic Expressions;349
19.1.5;Simplifying Algebraic Expressions;351
19.1.6;Symbolic Differentiation of Algebraic Expressions;353
19.1.7;Rendering Expressions;354
19.1.8;Converting to VisualExpr;356
19.2;Verifying Circuits with Propositional Logic;362
19.2.1;Representing Propositional Logic;363
19.2.2;Evaluating Propositional Logic Naively;364
19.2.3;From Circuits to Propositional Logic;366
19.2.4;Checking Simple Properties of Circuits;369
19.2.5;Representing Propositional Formulae Efficiently Using BDDs;370
19.2.6;Circuit Verification with BDDs;374
19.3;Summary;376
20;CHAPTER 13 Reactive, Asynchronous, andParallel Programming;377
20.1;Introducing Some Terminology;378
20.2;Using and Designing Background Workers;379
20.2.1;Building a Simpler Iterative Worker;381
20.2.2;Raising Additional Events from Background Workers;384
20.2.3;Connecting a Background Worker to a GUI;385
20.3;Introducing Asynchronous and Parallel Computations;387
20.3.1;Fetching Multiple Web Pages in Parallel, Asynchronously;387
20.3.2;Understanding Thread Hopping;389
20.3.3;Under the Hood: What Are Asynchronous Computations?;390
20.3.4;Parallel File Processing Using Asynchronous Workflows;392
20.3.5;Running Asynchronous Computations;396
20.3.6;Common I/O Operations in Asynchronous Workflows;396
20.3.7;Under the Hood: Implementing Async.Parallel;397
20.3.8;Using async for CPU Parallelism with Fixed Tasks;398
20.3.9;Understanding Exceptions and Cancellation;398
20.4;Passing and Processing Messages;399
20.4.1;Introducing Message Processing;400
20.4.2;Creating Objects That React to Messages;401
20.4.3;Scanning Mailboxes for Relevant Messages;404
20.4.4;Example: Asynchronous Web Crawling;405
20.5;Using Shared-Memory Concurrency;407
20.5.1;Creating Threads Explicitly;408
20.5.2;Shared Memory, Race Conditions, and the .NET Memory Model;408
20.5.3;Using Locks to Avoid Race Conditions;409
20.5.4;Using ReaderWriterLock;410
20.5.5;Some Other Concurrency Primitives;411
20.6;Summary;411
21;CHAPTER 14Building Smart Web Applications;412
21.1;Serving Static Web Content;412
21.2;Serving Dynamic Web Content with ASP.NET;416
21.2.1;Understanding the Languages Used in ASP.NET;418
21.2.2;A Simple ASP.NET Web Application;420
21.2.3;Deploying and Running the Application;423
21.2.4;Using Code-Behind Files;425
21.3;Using ASP.NET Input Controls;426
21.4;Displaying Data from Databases;429
21.5;Going Further with ASP.NET;433
21.5.1;ASP.NET Directives;433
21.5.2;Server Controls;434
21.5.3;Debugging, Profiling, and Tracing;436
21.5.4;Understanding the ASP.NET Event Model;437
21.5.5;Maintaining the View State;439
21.5.6;Understanding the Provider Model;440
21.5.7;Creating Custom ASP.NET Server Controls;441
21.6;Building Ajax Rich Client Applications;443
21.6.1;More on the WebSharper Platform;443
21.7;Using WSDL Web Services;451
21.7.1;Consuming Web Services;451
21.7.2;Calling Web Services Asynchronously;454
21.8;Summary;455
22;CHAPTER 15Working with Data;456
22.1;Querying In-Memory Data Structures;456
22.1.1;Select/Where/From Queries Using Aggregate Operators;457
22.1.2;Using Aggregate Operators in Queries;457
22.1.3;Accumulating Using Folding Operators;458
22.1.4;Expressing Some Queries Using Sequence Expressions;459
22.2;Using Databases to Manage Data;460
22.2.1;Choosing Your Database Engine;462
22.2.2;Understanding ADO.NET;463
22.2.3;Establishing Connections to a Database Engine;464
22.2.4;Creating a Database;464
22.2.5;Creating Tables and Inserting and Fetching Records;466
22.2.6;Using Untyped Datasets;468
22.2.7;Generating Typed Datasets Using xsd.exe;470
22.2.8;Using Stored Procedures;472
22.2.9;Using Data Grids;473
22.3;Working with Databases in Visual Studio;473
22.3.1;Creating a Database;473
22.3.2;Visual Data Modeling: Adding Relationships;474
22.4;Accessing Relational Data with Linq Queries;476
22.4.1;Generating the Object/Relational Mapping;476
22.4.2;Building the DataContext Instance;477
22.4.3;Using LINQ from F#;477
22.5;Working with XML as a Generic Data Format;479
22.5.1;Constructing XML via LINQ;480
22.5.2;Storing, Loading, and Traversing LinqToXml Documents;481
22.5.3;Querying XML;482
22.6;Summary;482
23;CHAPTER 16Lexing and Parsing;483
23.1;Processing Line-Based Input;484
23.1.1;On-Demand Reading of Files;485
23.1.2;Using Regular Expressions;485
23.2;Tokenizing with FsLex;486
23.2.1;The FsLex Input in More Detail;489
23.2.2;Generating a Simple Token Stream;490
23.2.3;Tracking Position Information Correctly;492
23.2.4;Handling Comments and Strings;493
23.3;Recursive-Descent Parsing;494
23.3.1;Limitations of Recursive-Descent Parsers;498
23.4;Parsing with FsYacc;499
23.4.1;The Lexer for Kitty;500
23.4.2;The Parser for Kitty;501
23.4.3;Parsing Lists;503
23.4.4;Resolving Conflicts, Operator Precedence, and Associativity;503
23.4.5;Putting It Together;504
23.5;Binary Parsing and Pickling Using Combinators;506
23.6;Summary;509
24;CHAPTER 17Interoperating with C and COM;510
24.1;Common Language Runtime;510
24.2;Memory Management at Runtime;512
24.3;COM Interoperability;514
24.4;Platform Invoke;526
24.4.1;Getting Started with PInvoke;528
24.4.2;Data Structures;529
24.4.3;Marshalling Strings;532
24.4.4;Function Pointers;535
24.4.5;PInvoke Memory Mapping;536
24.4.6;Wrapper Generation and Limits of PInvoke;539
24.5;Summary;540
25;CHAPTER 18 Debugging and Testing F#Programs;541
25.1;Debugging F# Programs;542
25.1.1;Using Advanced Features of the Visual Studio Debugger;544
25.1.2;Instrumenting Your Program with the System.DiagnosticsNamespace;546
25.1.3;Debugging Concurrent and Graphical Applications;549
25.2;Debugging and Testing with F# Interactive;551
25.2.1;Controlling F# Interactive;552
25.2.2;Some Common F# Interactive Directives;553
25.2.3;Understanding How F# Interactive Compiles Code;553
25.2.4;F# Interactive and Visual Studio;555
25.3;Unit Testing;556
25.4;Summary;561
26;CHAPTER 19Designing F# Libraries;562
26.1;Designing Vanilla .NET Libraries;563
26.2;Understanding Functional Design Methodology;567
26.3;Applying the .NET Library Design Guidelines to F#;570
26.4;Some Recommended Coding Idioms;577
26.5;Summary;578
27;APPENDIXF# Brief Language Guide;579
28;Index;586




