Expert F# 2.0 - Don Syme, Adam Granicz, Antonio Cisternino

Expert F# 2.0 (eBook)

eBook Download: PDF
2011 | 1st ed.
XXVI, 624 Seiten
Apress (Verlag)
978-1-4302-2432-7 (ISBN)
Systemvoraussetzungen
56,99 inkl. MwSt
  • Download sofort lieferbar
  • Zahlungsarten anzeigen

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.


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.

Copyright Page 3
Contents at a Glance 5
Table of Contents 7
Foreword 22
About the Authors 24
About the Technical Reviewer 25
Acknowledgments 26
CHAPTER 1 Introduction 27
The Genesis of F# 27
About This Book 28
Who This Book Is For 31
CHAPTER 2 Getting Started with F# and .NET 32
Creating Your First F# Program 32
Documenting Code Using XMLDocs 34
Using let 34
Understanding Types 35
Calling Functions 36
Lightweight Syntax 37
Understanding Scope 38
Using Data Structures 39
Using Properties and the Dot-Notation 40
Using Tuples 41
Using Imperative Code 43
Using .NET Libraries from F# 44
Using open to Access Namespaces and Modules 45
Using new and Setting Properties 46
Fetching a Web Page 47
Summary 49
CHAPTER 3 Creating Your First F# Program—Introducing FunctionalProgramming 50
Getting Started with F# Arithmetic 50
Basic Literals 50
Arithmetic Operators 51
Bitwise Operations 52
Arithmetic Conversions 53
Arithmetic Comparisons 54
Overloaded Math Functions 54
Introducing Simple Strings 55
Working with String Literals and Primitives 55
Building Strings 57
Working with Lists and Options 58
Using F# Lists 58
Using F# Option Values 62
Using Option Values for Control 63
Working with Conditionals: & &
Defining Recursive Functions 64
Introducing Function Values 65
Using Anonymous Function Values 66
Computing with Aggregate Operators 67
Composing Functions with > >
Building Functions with Partial Application 70
Using Local Functions 71
Using Functions as Abstract Values 72
Iterating with Aggregate Operators 73
Abstracting Control with Functions 73
Using .NET Methods as First-Class Functions 74
Getting Started with Pattern Matching 75
Matching on Structured Values 76
Guarding Rules and Combining Patterns 78
Getting Started with Sequences 78
Using Range Expressions 79
Iterating a Sequence 80
Transforming Sequences with Aggregate Operators 80
Which Types Can Be Used as Sequences? 81
Using Lazy Sequences from External Sources 82
Using Sequence Expressions 83
Creating Sequence Expressions Using for 83
Enriching Sequence Expressions with Additional Logic 84
Generating Lists and Arrays Using Sequence Expressions 85
Exploring Some Simple Type Definitions 85
Defining Type Abbreviations 85
Defining Records 86
Handling Non-Unique Record Field Names 87
Cloning Records 88
Defining Discriminated Unions 88
Using Discriminated Unions as Records 90
Defining Multiple Types Simultaneously 91
Summary 91
CHAPTER 4 Introducing ImperativeProgramming 92
Imperative Looping and Iterating 93
Simple for Loops 93
Simple while Loops 93
More Iteration Loops over Sequences 94
Using Mutable Records 94
Mutable Reference Cells 95
Avoiding Aliasing 97
Hiding Mutable Data 98
Using Mutable Locals 99
Working with Arrays 99
Generating and Slicing Arrays 101
Two-Dimensional Arrays 102
Introducing the Imperative .NET Collections 102
Using Resizeable Arrays 103
Using Dictionaries 104
Using Dictionary’s TryGetValue 104
Using Dictionaries with Compound Keys 105
Some Other Mutable Data Structures 106
Exceptions and Controlling Them 106
Catching Exceptions 108
Using try . . . finally 108
Defining New Exception Types 109
Having an Effect: Basic I/O 110
Very Simple I/O: Reading and Writing Files 110
.NET I/O via Streams 111
Some Other I/O-Related Types 112
Using System.Console 113
Using printf and Friends 113
Generic Structural Formatting 115
Cleaning Up with IDisposable, use, and using 115
Working with null Values 116
Some Advice: Functional Programming with Side Effects 117
Consider Replacing Mutable Locals and Loops with Recursion 118
Separate Pure Computation from Side-Effecting Computations 118
Separate Mutable Data Structures 118
Not All Side Effects Are Equal 119
Avoid Combining Imperative Programming and Laziness 120
Summary 121
CHAPTER 5Mastering Types and Generics 122
Understanding Generic Type Variables 122
Writing Generic Functions 123
Understanding Some Important Generic Functions 124
Generic Comparison 124
Generic Hashing 125
Generic Pretty-Printing 126
Generic Boxing and Unboxing 126
Generic Binary Serialization via the .NET Libraries 127
Making Things Generic 129
Generic Algorithms through Explicit Arguments 129
Generic Algorithms through Abstract Object Types 131
Arithmetic Operators and Generic Algorithms through Inlining 133
Understanding .NET Types 134
Reference Types and Value Types 134
Other Flavors of .NET Types 135
Understanding Subtyping 136
Casting Up Statically 136
Casting Down Dynamically 136
Performing Type Tests via Pattern Matching 137
Knowing When Upcasts Are Applied Automatically 138
Flexible Types 139
Units of Measure 140
Troubleshooting Type-Inference Problems 142
Using a Visual Editing Environment 142
Using Type Annotations 142
Understanding the Value Restriction 143
Working Around the Value Restriction 144
Understanding Generic Overloaded Operators 146
Summary 147
CHAPTER 6Working with Objects and Modules 148
Getting Started with Objects and Members 148
Using Classes 151
Adding Further Object Notation to Your Types 155
Working with Indexer Properties 155
Adding Overloaded Operators 155
Using Named and Optional Arguments 157
Using Optional Property Settings 158
Adding Method Overloading 159
Defining Object Types with Mutable State 160
Getting Started with Object Interface Types 162
Defining New Object Interface Types 163
Implementing Object Interface Types Using Object Expressions 164
Implementing Object Interface Types Using Concrete Types 165
Using Common Object Interface Types from the .NET Libraries 166
Understanding Hierarchies of Object Interface Types 167
More Techniques to Implement Objects 168
Combining Object Expressions and Function Parameters 168
Defining Partially Implemented Class Types 169
Using Partially Implemented Types via Delegation 170
Using Partially Implemented Types via Implementation Inheritance 170
Using Modules and Static Members 172
Extending Existing Types and Modules 173
Working with F# Objects and .NET Types 175
Structs 176
Delegates 176
Enums 176
Summary 177
CHAPTER 7 Encapsulating and PackagingYour Code 178
Hiding Things Away 178
Hiding Things with Local Definitions 179
Hiding Things with Accessibility Annotations 181
Using Namespaces and Modules 184
Putting Your Code in a Namespace 184
Using Files as Modules 185
Creating Assemblies, DLLs, and EXEs 186
Compiling EXEs 186
Compiling DLLs 186
Mixing Scripting and Compiled Code 188
Choosing Optimization Settings 189
Generating Documentation 189
Building Shared Libraries and Using the Global Assembly Cache 190
Using Static Linking 191
Using Signature Types and Files 192
Using Explicit Signature Types and Signature Files 192
When Are Signature Types Checked? 193
Packaging Applications 194
Packaging Different Kinds of Code 194
Using Data and Configuration Settings 195
Summary 198
CHAPTER 8Mastering F#: Common Techniques 199
Equality, Hashing, and Comparison 199
Asserting Equality, Hashing, and Comparison Using Attributes 200
Fully Customizing Equality, Hashing, and Comparison on a Type 201
Suppressing Equality, Hashing, and Comparison on a Type 203
Customizing Generic Collection Types 203
Efficient Precomputation and Caching 204
Precomputation and Partial Application 204
Precomputation and Objects 206
Memoizing Computations 207
Lazy Values 209
Other Variations on Caching and Memoization 210
Cleaning Up Resources 210
Cleaning Up with use 211
Managing Resources with More Complex Lifetimes 213
Cleaning Up Internal Objects 214
Cleaning Up Unmanaged Objects 215
Cleaning Up in Sequence Expressions 216
Using using 218
Stack as a Resource: Tail Calls and Recursion 218
Tail Recursion and List Processing 219
Tail Recursion and Object-Oriented Programming 221
Tail Recursion and Processing Unbalanced Trees 222
Using Continuations to Avoid Stack Overflows 223
Another Example: Processing Syntax Trees 225
Events 226
Events as First-Class Values 227
Creating and Publishing Events 228
Summary 229
CHAPTER 9 Introducing Language-OrientedProgramming 230
Using XML as a Concrete Language Format 231
Using the System.Xml Namespace 231
From Concrete XML to Abstract Syntax 233
Working with Abstract Syntax Representations 236
Abstract Syntax Representations: Less Is More 236
Processing Abstract Syntax Representations 236
Transformational Traversals of Abstract Syntax Representations 238
Using On-Demand Computation with Abstract Syntax Trees 238
Caching Properties in Abstract Syntax Trees 240
Memoizing Construction of Syntax Tree Nodes 241
Introducing Active Patterns 243
Converting the Same Data to Many Views 243
Matching on .NET Object Types 245
Defining Partial and Parameterized Active Patterns 246
Hiding Abstract Syntax Implementations with Active Patterns 246
Embedded Computational Languages with Workflows 248
An Example: Success/Failure Workflows 250
Defining a Workflow Builder 253
Workflows and Untamed Side Effects 255
Example: Probabilistic Workflows 256
Combining Workflows and Resources 261
Recursive Workflow Expressions 261
Using F# Reflection 262
Reflecting on Types 262
Schema Compilation by Reflecting on Types 262
Using the F# Dynamic Reflection Operators 266
Using F# Quotations 267
Example: Using F# Quotations for Error Estimation 268
Resolving Top Definitions 270
Summary 271
CHAPTER 10Using the F# and .NET Libraries 272
A High-Level Overview 272
Namespaces from the .NET Framework 273
Namespaces from the F# Libraries 275
Using the System Types 276
Using Regular Expressions and Formatting 278
Matching with System.Text.RegularExpressions 278
Formatting Strings Using .NET Formatting 283
Encoding and Decoding Unicode Strings 284
Encoding and Decoding Binary Data 285
Using Further F# and .NET Data Structures 285
System.Collections.Generic and Other .NET Collections 285
Introducing Microsoft.FSharp.Math 286
Using Matrices and Vectors 286
Using Operator Overloads on Matrices and Vectors 288
Supervising and Isolating Execution 288
Further Libraries for Reflective Techniques 289
Using General Types 289
Using Microsoft.FSharp.Reflection 290
Some Other .NET Types You May Encounter 290
Some Other .NET Libraries 291
Summary 292
CHAPTER 11Building Graphical User Interfaces 293
Writing “Hello, World!” in a Click 293
Understanding the Anatomy of a Graphical Application 294
Composing User Interfaces 295
Drawing Applications 300
Writing Your Own Controls 304
Developing a Custom Control 304
Anatomy of a Control 307
Displaying Samples from Sensors 308
Building the GraphControl: The Model 310
Building the GraphControl: Style Properties and Controller 312
Building the GraphControl: The View 315
Putting It Together 319
Creating a Mandelbrot Viewer 320
Computing Mandelbrot 321
Setting Colors 322
Creating the Visualization Application 325
Creating the Application Plumbing 327
Windows Presentation Foundation 331
When GUIs Meet the Web 333
Drawing 334
Controls 337
Bitmaps and Images 340
Final Considerations 342
Summary 342
CHAPTER 12 Working with SymbolicRepresentations 343
Symbolic Differentiation and Expression Rendering 344
Modeling Simple Algebraic Expressions 344
Implementing Local Simplifications 346
A Richer Language of Algebraic Expressions 347
Parsing Algebraic Expressions 349
Simplifying Algebraic Expressions 351
Symbolic Differentiation of Algebraic Expressions 353
Rendering Expressions 354
Converting to VisualExpr 356
Verifying Circuits with Propositional Logic 362
Representing Propositional Logic 363
Evaluating Propositional Logic Naively 364
From Circuits to Propositional Logic 366
Checking Simple Properties of Circuits 369
Representing Propositional Formulae Efficiently Using BDDs 370
Circuit Verification with BDDs 374
Summary 376
CHAPTER 13 Reactive, Asynchronous, andParallel Programming 377
Introducing Some Terminology 378
Using and Designing Background Workers 379
Building a Simpler Iterative Worker 381
Raising Additional Events from Background Workers 384
Connecting a Background Worker to a GUI 385
Introducing Asynchronous and Parallel Computations 387
Fetching Multiple Web Pages in Parallel, Asynchronously 387
Understanding Thread Hopping 389
Under the Hood: What Are Asynchronous Computations? 390
Parallel File Processing Using Asynchronous Workflows 392
Running Asynchronous Computations 396
Common I/O Operations in Asynchronous Workflows 396
Under the Hood: Implementing Async.Parallel 397
Using async for CPU Parallelism with Fixed Tasks 398
Understanding Exceptions and Cancellation 398
Passing and Processing Messages 399
Introducing Message Processing 400
Creating Objects That React to Messages 401
Scanning Mailboxes for Relevant Messages 404
Example: Asynchronous Web Crawling 405
Using Shared-Memory Concurrency 407
Creating Threads Explicitly 408
Shared Memory, Race Conditions, and the .NET Memory Model 408
Using Locks to Avoid Race Conditions 409
Using ReaderWriterLock 410
Some Other Concurrency Primitives 411
Summary 411
CHAPTER 14Building Smart Web Applications 412
Serving Static Web Content 412
Serving Dynamic Web Content with ASP.NET 416
Understanding the Languages Used in ASP.NET 418
A Simple ASP.NET Web Application 420
Deploying and Running the Application 423
Using Code-Behind Files 425
Using ASP.NET Input Controls 426
Displaying Data from Databases 429
Going Further with ASP.NET 433
ASP.NET Directives 433
Server Controls 434
Debugging, Profiling, and Tracing 436
Understanding the ASP.NET Event Model 437
Maintaining the View State 439
Understanding the Provider Model 440
Creating Custom ASP.NET Server Controls 441
Building Ajax Rich Client Applications 443
More on the WebSharper Platform 443
Using WSDL Web Services 451
Consuming Web Services 451
Calling Web Services Asynchronously 454
Summary 455
CHAPTER 15Working with Data 456
Querying In-Memory Data Structures 456
Select/Where/From Queries Using Aggregate Operators 457
Using Aggregate Operators in Queries 457
Accumulating Using Folding Operators 458
Expressing Some Queries Using Sequence Expressions 459
Using Databases to Manage Data 460
Choosing Your Database Engine 462
Understanding ADO.NET 463
Establishing Connections to a Database Engine 464
Creating a Database 464
Creating Tables and Inserting and Fetching Records 466
Using Untyped Datasets 468
Generating Typed Datasets Using xsd.exe 470
Using Stored Procedures 472
Using Data Grids 473
Working with Databases in Visual Studio 473
Creating a Database 473
Visual Data Modeling: Adding Relationships 474
Accessing Relational Data with Linq Queries 476
Generating the Object/Relational Mapping 476
Building the DataContext Instance 477
Using LINQ from F# 477
Working with XML as a Generic Data Format 479
Constructing XML via LINQ 480
Storing, Loading, and Traversing LinqToXml Documents 481
Querying XML 482
Summary 482
CHAPTER 16Lexing and Parsing 483
Processing Line-Based Input 484
On-Demand Reading of Files 485
Using Regular Expressions 485
Tokenizing with FsLex 486
The FsLex Input in More Detail 489
Generating a Simple Token Stream 490
Tracking Position Information Correctly 492
Handling Comments and Strings 493
Recursive-Descent Parsing 494
Limitations of Recursive-Descent Parsers 498
Parsing with FsYacc 499
The Lexer for Kitty 500
The Parser for Kitty 501
Parsing Lists 503
Resolving Conflicts, Operator Precedence, and Associativity 503
Putting It Together 504
Binary Parsing and Pickling Using Combinators 506
Summary 509
CHAPTER 17Interoperating with C and COM 510
Common Language Runtime 510
Memory Management at Runtime 512
COM Interoperability 514
Platform Invoke 526
Getting Started with PInvoke 528
Data Structures 529
Marshalling Strings 532
Function Pointers 535
PInvoke Memory Mapping 536
Wrapper Generation and Limits of PInvoke 539
Summary 540
CHAPTER 18 Debugging and Testing F#Programs 541
Debugging F# Programs 542
Using Advanced Features of the Visual Studio Debugger 544
Instrumenting Your Program with the System.DiagnosticsNamespace 546
Debugging Concurrent and Graphical Applications 549
Debugging and Testing with F# Interactive 551
Controlling F# Interactive 552
Some Common F# Interactive Directives 553
Understanding How F# Interactive Compiles Code 553
F# Interactive and Visual Studio 555
Unit Testing 556
Summary 561
CHAPTER 19Designing F# Libraries 562
Designing Vanilla .NET Libraries 563
Understanding Functional Design Methodology 567
Applying the .NET Library Design Guidelines to F# 570
Some Recommended Coding Idioms 577
Summary 578
APPENDIXF# Brief Language Guide 579
Index 586

Erscheint lt. Verlag 22.4.2011
Zusatzinfo XXVI, 624 p.
Verlagsort Berkeley
Sprache englisch
Themenwelt Mathematik / Informatik Informatik Programmiersprachen / -werkzeuge
Mathematik / Informatik Informatik Software Entwicklung
Schlagworte Concurrent programming • Debugging • F# • Functional Programming • .NET • Parallel Programming • programming • Programming language
ISBN-10 1-4302-2432-0 / 1430224320
ISBN-13 978-1-4302-2432-7 / 9781430224327
Haben Sie eine Frage zum Produkt?
PDFPDF (Wasserzeichen)
Größe: 5,8 MB

DRM: Digitales Wasserzeichen
Dieses eBook enthält ein digitales Wasser­zeichen und ist damit für Sie persona­lisiert. Bei einer missbräuch­lichen Weiter­gabe des eBooks an Dritte ist eine Rück­ver­folgung an die Quelle möglich.

Dateiformat: PDF (Portable Document Format)
Mit einem festen Seiten­layout eignet sich die PDF besonders für Fach­bücher mit Spalten, Tabellen und Abbild­ungen. Eine PDF kann auf fast allen Geräten ange­zeigt werden, ist aber für kleine Displays (Smart­phone, eReader) nur einge­schränkt geeignet.

Systemvoraussetzungen:
PC/Mac: Mit einem PC oder Mac können Sie dieses eBook lesen. Sie benötigen dafür einen PDF-Viewer - z.B. den Adobe Reader oder Adobe Digital Editions.
eReader: Dieses eBook kann mit (fast) allen eBook-Readern gelesen werden. Mit dem amazon-Kindle ist es aber nicht kompatibel.
Smartphone/Tablet: Egal ob Apple oder Android, dieses eBook können Sie lesen. Sie benötigen dafür einen PDF-Viewer - z.B. die kostenlose Adobe Digital Editions-App.

Zusätzliches Feature: Online Lesen
Dieses eBook können Sie zusätzlich zum Download auch online im Webbrowser lesen.

Buying eBooks from abroad
For tax law reasons we can sell eBooks just within Germany and Switzerland. Regrettably we cannot fulfill eBook-orders from other countries.

Mehr entdecken
aus dem Bereich
Das umfassende Handbuch

von Johannes Ernesti; Peter Kaiser

eBook Download (2023)
Rheinwerk Computing (Verlag)
44,90
Das Handbuch für Webentwickler

von Philip Ackermann

eBook Download (2023)
Rheinwerk Computing (Verlag)
49,90