CYAN MAGENTA YELLOW BLACK PANTONE 123 CV this print for content only—size & color not accurate 7" x 9-1/4" / CASEBOUND / MALLOY (1.0 INCH BULK 488 pages 50# Thor) THE EXPERT’S VOICE ® IN OPEN SOURCE Joshua B. Smith Practical OCaml BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® Shelve in Programming Languages User level: Beginner–Intermediate Practical OCaml Smith ISBN 1-59059-620-X 9 781590 596203 90000 6 89253 59620 3 Companion eBook Available RELATED TITLES forums.apress.com FOR PROFESSIONALS BY PROFESSIONALS ™ Join online discussions: www.apress.com SOURCE CODE ONLINE Companion eBook See last page for details on $10 eBook version Practical OCaml Dear Reader, OCaml, like many functional programming languages, has historically managed to maintain a low profile despite widespread use in industries ranging from microchip fabrication to financial analysis. Yet with functional programming recently experiencing a renaissance of sorts, interest in this powerful language is picking up sharply. And for good reason. Sporting features such as a robust object system, type safety, and an expansive standard library, OCaml is a language that encourages pragmatic instead of dogmatic solutions. I wrote this book to teach you all about this general-purpose language’s won- derful array of talents. Beginning with an overview of the OCaml installation process and toplevel, the interactive OCaml interpreter, you’ll have a solid framework in place for investigating the language’s syntax, semantics, and types in the chapters that follow. You’ll also learn about advanced features such as the ocamldoc documentation-generation tool, threading, and the Camlp4 preprocessor and pretty printer. Along the way, you’ll apply what you’ve learned by implementing a number of practical OCaml-driven applications. I’ve omitted the typical and tired Towers of Hanoi and four-function calculator examples. Instead, I choose to focus on building applications capable of performing complex log-file processing, web crawling, filtering spam, and even broadcasting audio over a network using the Shoutcast protocol. Adding to the excitement, I’ll occasionally delve into tangential topics such as OCaml’s impurities from the functional programming perspective and show you how to overcome them by using mutability, references, and classes. Joshua B. Smith Master the OCaml functional programming language by building an audio server, web crawler, spam filter, and other practical projects! Joshua B. Smith Practical OCaml 620Xfmfinal.qxd 9/22/06 4:21 PM Page i P ractical OCaml 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 2 1 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 orders-ny@springer-sbm.com, 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 info@apress.com, 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 directly or indirectly 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. 620Xfmfinal.qxd 9/22/06 4:21 PM Page ii For Carol and Aaron 620Xfmfinal.qxd 9/22/06 4:21 PM Page iii Contents at a Glance About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv ■CHAPTER 1 Why Objective Caml (OCaml)?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 ■CHAPTER 2 Interacting with OCaml: The Toplevel. . . . . . . . . . . . . . . . . . . . . . . . . . . 11 ■CHAPTER 3 Syntax and Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 ■CHAPTER 4 Understanding Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 ■CHAPTER 5 Practical: Creating a Simple Database. . . . . . . . . . . . . . . . . . . . . . . . . . 51 ■CHAPTER 6 Primitive and Composite Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 ■CHAPTER 7 Practical: Simple Database Reports, Exports, and Imports . . . . . . 73 ■CHAPTER 8 Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 ■CHAPTER 9 Files and File I/O. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 ■CHAPTER 10 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 ■CHAPTER 11 Practical: A URI Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 ■CHAPTER 12 Using Ocamldoc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 ■CHAPTER 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 ■CHAPTER 17 Practical: Complex Log File Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 ■CHAPTER 18 The Objective Part of Caml. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 ■CHAPTER 19 Dig r ession: OCaml Is Not Pure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 ■CHAPTER 20 Dig r ession: Functional Prog r amming . . . . . . . . . . . . . . . . . . . . . . . . . . 261 ■CHAPTER 21 Practical: Web Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 ■CHAPTER 22 Practical: A Shoutcast Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 ■CHAPTER 23 Using Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 ■CHAPTER 24 Practical: A Concurrent Web Crawler . . . . . . . . . . . . . . . . . . . . . . . . . . 329 ■CHAPTER 25 Interfacing with OCaml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 iv 620Xfmfinal.qxd 9/22/06 4:21 PM Page iv ■CHAPTER 26 Practical: Time and Logging Libraries . . . . . . . . . . . . . . . . . . . . . . . . . 359 ■CHAPTER 27 Processing Binary Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 ■CHAPTER 28 OCaml Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401 ■CHAPTER 29 Camlp4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411 ■CHAPTER 30 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431 ■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 v 620Xfmfinal.qxd 9/22/06 4:21 PM Page v 620Xfmfinal.qxd 9/22/06 4:21 PM Page vi Contents About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv ■CHAPTER 1 Why Objective Caml (OCaml)? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Who Benefits from Learning OCaml?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 What Is OCaml Good F or? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Who Uses OCaml?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Where Did OCaml Come From? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Wha t Is the Current State of the Art? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Why This Book?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 What Is Covered?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Interacting with OCaml: The Toplevel (Chapter 2) . . . . . . . . . . . . . . . . 4 Syntax and Semantics (Chapter 3) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Understanding Functions (Chapter 4). . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Practical: Crea ting a Simple Database (Chapter 5) . . . . . . . . . . . . . . . 5 Primitive and Composite Types (Chapter 6) . . . . . . . . . . . . . . . . . . . . . 5 Practical: Simple Database Reports, Exports, and Imports (Cha pter 7) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Collections (Cha pter 8) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Files and File I/O (Chapter 9) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Exception Handling (Chapter 10). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Practical: A URI Librar y (Chapter 11) . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Using OCamldoc (Chapter 12) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Modules and Functors (Cha pter 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 Caml (Chapter 18) . . . . . . . . . . . . . . . . . . . . . . . . 7 Digression: OCaml Is Not Pure (Chapter 19) . . . . . . . . . . . . . . . . . . . . . 7 Digression: Functional Programming (Cha pter 20) . . . . . . . . . . . . . . . 7 vii 620Xfmfinal.qxd 9/22/06 4:21 PM Page vii 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 OCaml (Chapter 25) . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Practical: Time and Logging Libraries (Chapter 26). . . . . . . . . . . . . . . 8 Practical: Processing Binary Files (Chapter 27) . . . . . . . . . . . . . . . . . . 8 OCaml 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 OCaml: The Toplevel . . . . . . . . . . . . . . . . . . . . . 11 Distributions of OCaml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Official Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Unofficial Releases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Installing OCaml. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Interacting with the Toplevel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Hello World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Final Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Code F iles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Basic Code Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Custom Toplevel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Why Would You Do This? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Ho w Do You Do This? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 ■CHAPTER 3 Syntax and Semantics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 V ariables in a Constant Langua ge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Wha t Variables Are Not . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Let Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Understanding Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 ■CONTENTSviii 620Xfmfinal.qxd 9/22/06 4:21 PM Page viii Records and Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Creating Enums and Simple User-Defined Types . . . . . . . . . . . . . . . 26 Defining Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 More About Math. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Integers and Floats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Others . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Defining Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Imperative Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Signatures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 ■CHAPTER 4 Understanding Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Creating Values and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Functions Must Ha ve 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 Crea ting 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 Ma tch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Bindings Within Pattern Matches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Guarded Ma tches: A Return to the Distance Calcula tor . . . . . . . . . . 48 Understanding Built-in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Using Labels and Optional Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Conc lusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 ■CONTENTS ix 620Xfmfinal.qxd 9/22/06 4:21 PM Page ix [...]... and Ocaml programming Interfacing with OCaml (Chapter 25) This chapter is a brief tutorial on CamlIDL and wrapper writing in C for OCaml directly We also talk about why FFI matters in a language such as OCaml 7 620Xch01final.qxd 8 9/22/06 12:11 AM Page 8 CHAPTER 1 s WHY OBJECTIVE CAML (OCAML) ? Practical: Time and Logging Libraries (Chapter 26) Now that there is an understanding of how to interface Ocaml. .. OCaml What Isn’t Covered? This book is more than just a tutorial I have tried to cover all of the areas of normal OCaml programming We will not be covering extending the OCaml language in languages other than Ocaml and C If that sounds confusing, don’t worry about it, it is Much of the OCaml distribution is written in OCaml, but there are ways of writing your own functions in languages other than Ocaml. .. Uses OCaml? This is often the second question people ask about OCaml The answer is this: a lot of people From hedge fund users to graduate students, the list of people using OCaml to solve problems grows every day Airbus and Microsoft are two of the many companies that use OCaml to help avoid problems in programs written in languages other than OCaml The shopping engine for NBCi is written in OCaml. .. OCaml Filename module) and has stubs for handling other URI types as well Using OCamldoc (Chapter 12) Ocaml includes literate programming features with its documentation-generation tool, Ocamldoc This is a short chapter that discusses the OCamldoc tool We give examples in Chapter 11, but here we discuss the specifics of OCamldoc and how to make the documentation better Modules and Functors (Chapter... provides a basic server to complete the application Ocamllex and Ocamlyacc (Chapter 16) This is a basic introduction that covers the differences between Lex and Yacc It features an example, but not a four-function calculator This chapter does not cover Abstract Syntax Trees (ASTs) Practical: Complex Log File Parsing (Chapter 17) Using ocamllex and ocamlyacc, we create a fast and flexible log file scanner... 9/22/06 12:11 AM Page 7 CHAPTER 1 s WHY OBJECTIVE CAML (OCAML) ? The Objective Part of Caml (Chapter 18) As you might expect, Objective Caml includes a robust and complete object system that is integrated with the Ocaml type system This chapter discusses the use and limitations of objects (and OOP) in Ocaml Digression: OCaml Is Not Pure (Chapter 19) OCaml is not a pure functional programming language We... that buffer overruns and similar runtime failures cannot happen in OCaml programs Safety in OCaml extends beyond this, though The static typing and compile-time checks by the OCaml compiler make certain classes of errors impossible Type conversion or mismatch errors cannot happen in OCaml because automatic type coercions cannot happen OCaml code is also verifiable There are automated proof utilities... significant contributions to OCaml include mod_caml (bindings for Apache), perl4caml (using Perl code within OCaml) , PG OCaml (typesafe bindings for PostgreSQL), and the Merjis AdWords Toolkit xxiii 620Xfmfinal.qxd 9/22/06 4:21 PM Page xxiv 620Xfmfinal.qxd 9/22/06 4:21 PM Page xxv Acknowledgments W ithout the resources created and provided by the OCaml team and the larger OCaml community, this book would... s WHY OBJECTIVE CAML (OCAML) ? Syntax and Semantics (Chapter 3) This chapter covers types, records, and control flow We also introduce let bindings, variables, and comparison operators We cover them with an eye to the fact that OCaml is a constant language We spend some time talking about the math problem in OCaml, which is one of the more commonly complained-about aspects of OCaml: namely that the... CHAPTER 1 s WHY OBJECTIVE CAML (OCAML) ? Exception Handling (Chapter 10) This chapter discusses stack unwinding, exceptions, and all the scary stuff that isn’t so scary because OCaml is a constant language We also cover exception handling in classes Practical: A URI Library (Chapter 11) In this chapter, we implement a URI parsing library that handles file:// URIs (using the OCaml Filename module) and has . OPEN SOURCE Joshua B. Smith Practical OCaml BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® Shelve in Programming Languages User level: Beginner–Intermediate Practical OCaml Smith ISBN 1-59059-620-X 9. server, web crawler, spam filter, and other practical projects! Joshua B. Smith Practical OCaml 620Xfmfinal.qxd 9/22/06 4:21 PM Page i P ractical OCaml Copyright © 2006 by Joshua B. Smith All. . . 6 Practical: A Spam Filter (Chapter 14) . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Practical: A Network-Aware Scoring Function (Chapter 15). . . . . . . . 6 Ocamllex and Ocamlyacc