ebook img

Practical OCaml PDF

465 Pages·2007·5.97 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Practical OCaml

Practical OCamI zS 'AK Joshua B. Smith Apress' Practical OCamI Copyright €) 2006 by Joshua B. Smith All 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 retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13:978-1-59059-620-3 ISBN-10:1-59059-620-X Printed and bound in the United States of America 9 8 7 6 5 4 3 21 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Matt Wade Technical Reviewer: Richard Jones Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick, Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Jim Sumser, Keir Thomas, Matt Wade Project Manager: Sofia Marchant Copy Edit Manager: Nicole Flores Copy Editor: Nancy Sixsmith Assistant Production Director: Kari Brooks-Copony Production Editor: Katie Stence Compositor: Linda Weidemann, Wolf Creek Press Proofreader: April Eddy Indexer: Brenda Miller Artist: April Milne Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed 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 [email protected], or visit http: //www. springeronline. com. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com. The information in this book is distributed on an "as is" basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author (s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directiy or indirectiy by 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. For Carol and Aaron Contents at a Glance About the Author xxi About the Technical Reviewer xxlli Acknowledgments xxv ICHAPTER 1 Why Objective CamI (OCaml)? i CHAPTER 2 Interacting with OCaml: The Toplevel 11 MJHAPTER 3 Syntax and Semantics 21 ICHAPTER 4 Understanding Functions 33 ^CHAPTER 5 Practical: Creating a Simple Database 51 •CHAPTER 6 Primitive and Composite Types 6I ICHAPTER 7 Practical: Simple Database Reports, Exports, and Imports 73 «)HAPTER 8 Collections 89 ^CHAPTER 9 Files and File I/O 113 ICHAPTER 10 Exception Handling 123 ICHAPTER 11 Practical: A URI Library 135 ICHAPTER 12 Using Ocamldoc 145 MIHAPTER 13 Modules and Functors 155 CHAPTER 14 Practical: A Spam Filter 169 ^CHAPTER 15 Practical: A Network-Aware Scoring Function 179 •CHAPTER 16 Ocamllex and Ocamlyacc 193 fCHAPTER 17 Practical: Complex Log File Parsing 213 ICHAPTER 18 The Objective Part of CamI 225 ICHAPTER 19 Digression: OCaml Is Not Pure 249 frCHAPTER 20 Digression: Functional Programming 26I %H€ APTER 21 Practical: Web Programming 273 tCHAPTER 22 Practical: A Shoutcast Server 293 -CHAPTER 23 Using Threads 309 SCHAPTER 24 Practical: A Concurrent Web Crawler 329 KHAPTER 25 Interfacing with OCaml 349 iCHAPTER 26 Practical: Time and Logging Libraries 359 SHAPTER 27 Processing Binary Files 375 ffllHAPTER 28 OCamI Development Tools 40i CHAPTER 29 Camlp4 411 9)HAPTER 30 Conclusion 431 aNDEX 445 Contents About the Author xxi About the Technical Reviewer xxiii Acknowledgments xxv CHAPTER 1 Why Objective CamI (OCamI)? 1 Who Benefits from Learning OCamI? 1 What Is OCamI Good For? 2 Who Uses OCamI? 3 Where Did OCamI Come From? 3 What Is the Current State of the Art? 4 Why This Book? 4 What Is Covered? 4 Interacting with OCamI: The Toplevel (Chapter 2) 4 Syntax and Semantics (Chapter 3) 5 Understanding Functions (Chapter 4) 5 Practical: Creating a Simple Database (Chapter 5) 5 Primitive and Composite Types (Chapter 6) 5 Practical: Simple Database Reports, Exports, and Imports (Chapter 7) 5 Collections (Chapter 8) 5 Files and File I/O (Chapter 9) 5 Exception Handling (Chapter 10) 6 Practical: A URI Library (Chapter 11) 6 Using OCamldoc (Chapter 12) 6 Modules and Functors (Chapter 13) 6 Practical: A Spam Filter (Chapter 14) 6 Practical: A Network-Aware Scoring Function (Chapter 15) 6 Ocamllex and Ocamlyacc (Chapter 16) 6 Practical: Complex Log File Parsing (Chapter 17) 6 The Objective Part of CamI (Chapter 18) 7 Digression: OCamI Is Not Pure (Chapter 19) 7 Digression: Functional Programming (Chapter 20) 7 VII CONTENTS Practical: Web Programming (Chapter 21) 7 Practical: A Shoutcast Server (Chapter 22) 7 Using Threads (Chapter 23) 7 Practical: A Concurrent Web Crawler (Chapter 24) 7 Interfacing with OCamI (Chapter 25) 7 Practical: Time and Logging Libraries (Chapter 26) 8 Practical: Processing Binary Files (Chapter 27) 8 OCamI Development Tools (Chapter 28) 8 Camlp4 (Chapter 29) 8 Conclusion (Chapter 30) 8 What Isn't Covered? 8 What Are My Expectations for You? 8 Conclusion 9 •CHAPTER 2 Interacting with OCamI: The Toplevel 11 Distributions of OCamI 12 Official Distribution 12 Unofficial Releases 12 Installing OCamI 12 Windows 12 Linux 14 Interacting with the Toplevel 16 Hello World 17 Final Notes 18 Code Files 18 Basic Code Files 19 Custom Toplevel 20 Why Would You Do This? 20 How Do You Do This? 20 Conclusion 20 •JHAPTER 3 Syntax and Semantics 21 Variables in a Constant Language 21 What Variables Are Not 21 Let Bindings 22 Understanding Scope 23 KONTENTS Records and Types 23 Creating Enums and Simple User-Defined Types 26 Defining Records 28 IVIore About Math 29 Integers and Floats 29 Others 29 Defining Functions 30 Imperative Programming 30 Recursion 30 Pattern Matching 31 Signatures 32 Conclusion 32 BHAPTER 4 Understanding Functions 33 Creating Values and Functions 33 Functions Must Have One Return Type 35 Constraining Types in Function Calls 35 Using Higher-Order Functions 36 Using Lists 37 Anonymous Functions 37 Why Use Anonymous Functions? 39 Understanding Consequences of Functions As Data 39 Curried Functions 41 Working with the Distance Type 41 Creating Recursive Functions 44 Why Do Recursive Functions Need a Special Designation? 45 Tail Recursion and Efficient Programming 45 Doing More Pattern Matching 46 Understanding the Default Match 47 Bindings Within Pattern Matches 47 Guarded Matches: A Return to the Distance Calculator 48 Understanding Built-in Functions 48 Using Labels and Optional Arguments 49 Conclusion 50 iCONTENTS M;HAPTER 5 Practical: Creating a Simple Database 51 Two Modules You Need 51 Using the Hashtbl Module in OCamI 51 Using the Marshal Module 52 How to Use These Modules 52 Working with Files 52 Accounts Database 53 Some Sample Data 53 Important Functions for Handling Data 54 Manipulating the Database Contents 54 Saving and Loading the Database 56 Interacting with the Database 56 Quick Note About Code Length 59 Getting Price Information 59 Conclusion 60 CHAPTER 6 Primitive and Composite Types 61 Constant Type, Dynamic Data 62 Integers (Ints) 62 Floating-Point Numbers (Floats) 63 Strings and Chars 64 Using the Pervasives Module 66 Lists and Arrays 66 Exceptions 66 Other Types 67 Polymorphic Types 67 Composite Types 68 Polymorphic Variant Types 69 Conclusion 71 lOONTENTS CHAPTER 7 Practical: Simple Database Reports, Exports, and Imports 73 Format Codes 73 Printf 74 fprintf 75 eprintf 75 printf 75 sprintf 75 bprintf 76 kprintf 76 Scant 76 Scanning Module 76 fscanf 76 scant 77 sscant 77 bscant 77 kscant 77 Why Use These Functions? 77 Why Not Regular Expressions? 77 What About Line-Oriented I/O? 78 The Right Tool for the Right Job 78 More About Buffers 80 Writing a Report 80 Writing Export Functions 83 Writing Import Functions 84 Generating Data 86 Conclusion 87 CHAPTER 8 Collections 89 What Are Collections? 90 Comparison Functions 90 Lists 91 Arrays and Matrices 97 Hashtables 100 Queue 103 Stack 105

Description:
Objective Caml (OCaml) is an open source programming language that allows you to utilize both functional and object-oriented programming. Sporting features such as a robust object system, type safety, and an expansive standard library, OCaml is a language that encourages pragmatic solutions instead
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.