Foundations of FSharp

383 452 0
Foundations of FSharp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Foundations of FSharp

this print for content only—size & color not accurate7" x 9-1/4" / CASEBOUND / MALLOY(0.9375 INCH BULK -- 384 pages -- 60# Thor)The eXPeRT’s VOIce® In .neTRobert PickeringForeword by Don Syme Foundations ofF#BOOks fOR PROfessIOnals By PROfessIOnals®Foundations of F#Dear Reader,I wrote this book because I believe functional programming (FP) is the future of .NET programming. You have already seen the influence of functional program-ming on the .NET Framework; everybody’s favorite .NET 2.0 feature, generics, is an idea lifted directly from FP, and F# creator Don Syme was the driving force behind the design and implementation of generics. To understand the full power of the current and future versions of the .NET Framework, every profes-sional .NET programmer needs to learn about FP, and there’s no better way to do that than by learning F#—and no easier way to learn F# than by reading Foundations of F#.If you’re already familiar with FP, you’ll find F# the language you’ve always dreamed of—it has all the power you would expect from an FP language as well as seamless access to the huge range of libraries and components that run on the .NET Framework. If you’re a .NET programmer, you will find F# to be an exciting real-world alternative to C# and Visual Basic. Thanks to F#’s concise and elegant syntax, it is an alternative in which programs can be expressed more clearly and in fewer lines of code. This book was reviewed for technical accuracy by F#’s inventor, Don Syme, who added his own touch of genius to many of the examples. This makes it an elegant, comprehensive introduction to all aspects of the language and an incisive guide to using F# for real-world professional development. It shows that F# is the future of programming (not just on .NET), and the future is now.Robert PickeringFoundations of F#Pickering cyan MaGenTa yellOW Black PanTOne 123 cISBN-13: 978-1-59059-757-6ISBN-10: 1-59059-757-59 781590 5975769 0 0 0 0Shelve in Programming LanguagesUser level: Beginner–Intermediatewww.apress.comSOURCE CODE ONLINECompanion eBook See last page for details on $10 eBook versionCompanion eBook AvailableRELATED TITLE Robert PickeringFoundations of F#7575FM.qxp 4/27/07 6:54 PM Page i Foundations of F#Copyright © 2007 by Robert PickeringAll rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,electronic or mechanical, including photocopying, recording, or by any information storage or retrievalsystem, without the prior written permission of the copyright owner and the publisher.ISBN-13: 978-1-59059-757-6ISBN-10: 1-59059-757-5Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrenceof a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademarkowner, with no intention of infringement of the trademark.Lead Editors: James Huddleston, Ewan BuckinghamTechnical Reviewer: Don SymeEditorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick,Jonathan Hassell, Chris Mills, Matthew Moodie, Jeffrey Pepper, Dominic Shakeshaft, Matt WadeProject Manager: Elizabeth SeymourCopy Edit Manager: Nicole FloresCopy Editor: Kim WimpsettAssistant Production Director: Kari Brooks-CoponyProduction Editor: Laura CheuCompositor: Lynn L’HeureuxProofreader: Elizabeth BerryIndexer: Broccoli Information ManagementArtist: April MilneCover Designer: Kurt KramesManufacturing Director: Tom DebolskiDistributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, orvisit http://www.springeronline.com.For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600,Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visithttp://www.apress.com.The information in this book is distributed on an “as is” basis, without warranty. Although every precautionhas been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability toany person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectlyby the information contained in this work. The source code for this book is available to readers at http://www.apress.com in the Source Code/Download section. 7575FM.qxp 4/27/07 6:54 PM Page ii For Susan and for Jim7575FM.qxp 4/27/07 6:54 PM Page iii 7575FM.qxp 4/27/07 6:54 PM Page iv Contents at a GlanceForeword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiiiAbout the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvAbout the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviiAcknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xixPreface: The Story of the Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi■CHAPTER 1 Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1■CHAPTER 2 How to Obtain, Install, and Use F#. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7■CHAPTER 3Functional Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15■CHAPTER 4 Imperative Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55■CHAPTER 5 Object-Oriented Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81■CHAPTER 6 Organizing, Annotating, and Quoting Code. . . . . . . . . . . . . . . . . . . . 111■CHAPTER 7 The F# Libraries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129■CHAPTER 8 User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167■CHAPTER 9 Data Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209■CHAPTER 10 Distributed Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239■CHAPTER 11 Language-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271■CHAPTER 12The F# Tool Suite and .NET Programming Tools. . . . . . . . . . . . . . . 299■CHAPTER 13 Compatibility and Advanced Interoperation. . . . . . . . . . . . . . . . . . . 323■INDEX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345v7575FM.qxp 4/27/07 6:54 PM Page v 7575FM.qxp 4/27/07 6:54 PM Page vi ContentsForeword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiiiAbout the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvAbout the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviiAcknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xixPreface: The Story of the Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi■CHAPTER 1Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1What Is Functional Programming? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Why Is Functional Programming Important? . . . . . . . . . . . . . . . . . . . . . . . . . 2What Is F#?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2Who Is Using F#?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3Who Is This Book For?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4What’s Next?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5■CHAPTER 2How to Obtain, Install, and Use F#. . . . . . . . . . . . . . . . . . . . . . . . . 7Obtaining F# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7Installing F# on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Installing F# on Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Using F# in Different Ways . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9Installing the Software Used in This Book . . . . . . . . . . . . . . . . . . . . . . 13Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14■CHAPTER 3Functional Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17Values and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26vii7575FM.qxp 4/27/07 6:54 PM Page vii Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28List Comprehensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33Types and Type Inference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Defining Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42Exceptions and Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . 49Lazy Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53■CHAPTER 4Imperative Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55The unit Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55The mutable Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57Defining Mutable Record Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59The ref Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62Array Comprehensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66Loops over Comprehensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69Calling Static Methods and Properties from .NET Libraries . . . . . . . . . . 69Using Objects and Instance Members from .NET Libraries . . . . . . . . . . 71Using Indexers from .NET Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . .73Working with Events from .NET Libraries . . . . . . . . . . . . . . . . . . . . . . . 74Pattern Matching over .NET Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76The |> Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .78Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .80■CHAPTER 5Object-Oriented Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81Type Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83Type Annotations for Subtyping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83Records As Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .85F# Types with Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88Object Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .90Defining Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93Implementing Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93Classes, Fields, and Explicit Constructors . . . . . . . . . . . . . . . . . . . . . . 95Implicit Class Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98Classes and Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99Classes and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101■CONTENTSviii7575FM.qxp 4/27/07 6:54 PM Page viii ■CONTENTSixAccessing the Base Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103Properties and Indexers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104Classes and Static Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106Overriding Methods from Non-F# Libraries . . . . . . . . . . . . . . . . . . . . . 107Defining Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108Structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108Enums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110■CHAPTER 6Organizing, Annotating, and Quoting Code. . . . . . . . . . . . . . 111Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112Opening Namespaces and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . 114Giving Namespaces and Modules Aliases . . . . . . . . . . . . . . . . . . . . . .115Signature Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116Module Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116Module Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .117Optional Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120Doc Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120Custom Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122Quoted Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .124Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127■CHAPTER 7The F# Libraries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129Libraries Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129The Native F# Library FSLib.dll . . . . . . . . . . . . . . . . . . . . . . . . . 130The ML Compatibility Library MLLib.dll . . . . . . . . . . . . . . . . . . . 131The Native F# Library FSLib.dll . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132The Microsoft.FSharp.Core.Operators Module . . . . . . . . . . . . . . 132The Microsoft.FSharp.Reflection Module . . . . . . . . . . . . . . . . . . 137The Microsoft.FSharp.Collections.Seq Module . . . . . . . . . . . . . .139The Microsoft.FSharp.Core.Enum Module . . . . . . . . . . . . . . . . . 149The Microsoft.FSharp.Text.Printf Module . . . . . . . . . . . . . . . . . . 151The Microsoft.FSharp.Control.IEvent Module . . . . . . . . . . . . . . . 154The Microsoft.FSharp.Math Namespace . . . . . . . . . . . . . . . . . . 156The ML Compatibility Library MLLib.dll . . . . . . . . . . . . . . . . . . . . . . . 160The Microsoft.FSharp.Compatibility.OCaml.Pervasives Module . . . 160The Microsoft.FSharp.Compatibility.OCaml.Arg Module . . . . . . . 164Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1667575FM.qxp 4/27/07 6:54 PM Page ix [...]... Microsoft .FSharp. Core.Operators Module . . . . . . . . . . . . . . 132 The Microsoft .FSharp. Reflection Module . . . . . . . . . . . . . . . . . . 137 The Microsoft .FSharp. Collections.Seq Module . . . . . . . . . . . . . . 139 The Microsoft .FSharp. Core.Enum Module . . . . . . . . . . . . . . . . . 149 The Microsoft .FSharp. T ext.Printf Module . . . . . . . . . . . . . . . . . . 151 The Microsoft .FSharp. Control.IEvent... 10,500 printf "Date of birth (ddmmyy): %i" x mathsPuzzle() The results of this example, when compiled and executed, are as follows: Enter day of the month on which you were born: 23 Enter number of the month you were born: 5 Enter last two digits of the year of your birth: 78 Date of birth (ddmmyy): 230578 I should note that this is different from changing the value of an identifier. Because... 384 pages 60# Thor) The eXPeRT’s VOIce ® In .neT Robert Pickering Foreword by Don Syme Foundations of F# BOOks fOR PROfessIOnals By PROfessIOnals ® Foundations of F# Dear Reader, I wrote this book because I believe functional programming (FP) is the future of .NET programming. You have already seen the influence of functional program- ming on the .NET Framework; everybody’s favorite .NET 2.0 feature,... implementation of generics. To understand the full power of the current and future versions of the .NET Framework, every profes- sional .NET programmer needs to learn about FP, and there’s no better way to do that than by learning F#—and no easier way to learn F# than by reading Foundations of F#. If you’re already familiar with FP, you’ll find F# the language you’ve always dreamed of it has all... inventor, Don Syme, who added his own touch of genius to many of the examples. This makes it an elegant, comprehensive introduction to all aspects of the language and an incisive guide to using F# for real-world professional development. It shows that F# is the future of programming (not just on .NET), and the future is now. Robert Pickering Foundations of F# Pickering cyan MaGenTa yellOW Black ... 4/27/07 6:54 PM Page ix Table 2-1. Continued Software Used In URL N Prof Chapter 12 h ttp://www.mertner.com/confluence/display/ NProf/Home CLR Profiler for .NET 2.0 Chapter 12 http://www.microsoft.com/downloads/details. aspx?familyid=a362781c-3870-43be-8926- 862b40aa0cd0&displaylang=e n Reflector Chapter 12 http://www.aisto.com/roeder/dotnet/ Obviously, some of these links are a little long to type,... Microsoft .FSharp. Control.IEvent Module . . . . . . . . . . . . . . . 154 The Microsoft .FSharp. Math Namespace . . . . . . . . . . . . . . . . . . 156 The ML Compatibility Library MLLib.dll . . . . . . . . . . . . . . . . . . . . . . . 160 The Microsoft .FSharp. Compatibility.OCaml.Pervasives Module . . . 160 The Microsoft .FSharp. Compatibility.OCaml.Arg Module . . . . . . . 164 Summary . . . . . . . .... these software packages, but for specific examples, as listed in Table 2-1, you’ll need to do this at some point. Table 2-1. Additional Software Used Within This Book Software Used In URL .NET Framework 3.0 Chapter 8, http://www.microsoft.com/downloads/details. Chapter 10 aspx?FamilyId=10CC340B-F857-4A14-83F5- 25634C3BF043&displaylang=en SDK for .NET Framework 3.0 Chapter 8, http://www.microsoft.com/downloads/details. Chapter... functions terminate, it is often useful to think of recursion in terms of a base case and the recursive case. The recursive case is the value for which the function is defined in terms of itself; for the function fib, this is any value other than 1 and 2. The base case is the nonrecursive case; that is, there must be some value where the function is not defined in terms of itself. In the fib function,... email) Finally, I talked to Jude O’Kelly, a software architect at Derivatives One, a company that sells financial modeling software, about why Derivatives One used F# in its products: We tested our financial models in both C# and F#; the performance was about the same, but we liked the F# versions because of the succinct mathematical syntax. One of our pr oblems with F# was the lack of information; we think this . .neTRobert PickeringForeword by Don Syme Foundations ofF#BOOks fOR PROfessIOnals By PROfessIOnals Foundations of F#Dear Reader,I wrote this book because. PickeringFoundations of F#7575FM.qxp 4/27/07 6:54 PM Page i Foundations of F#Copyright © 2007 by Robert PickeringAll rights reserved. No part of this

Ngày đăng: 21/08/2012, 09:28

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan