
At a Glance
488 Pages
23.5 x 18.42 x 3.18
Hardcover
$89.99
or 4 interest-free payments of $22.50 with
 orÂShips in 5 to 7 business days
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 of dogmatic ones. Boasting performance on par with the likes of C/C++, and having compilers available for a variety of platforms, including Windows, Unix, Linux, and Mac OS X, enterprise developers should consider adding this powerful language to their repertoire.
Written for experienced programmers, Practical OCaml teaches OCaml in a code-intensive fashion. Readers are encouraged to follow along with most examples using the OCaml top-level (the interactive interpreter), giving them the opportunity to consider the purpose and syntax of each line. The author's considerable knowledge of the Java, Python, and C++ languages allows him to present the material at a level and perspective that readers hailing from varied programming backgrounds will appreciate.
Language aficionados will be sure to enjoy the occasional digression into tangential topics such as OCaml's impurities from the functional programming perspective, with thoughts about how to overcome them using mutability, references, and classes.
In later chapters, you'll put what you've learned to work, building applications capable of performing complex log-file processing, crawling the Web, filtering spam, and even broadcasting audio over a network using the Shoutcast protocol.
| About the Author | p. xxi |
| About the Technical Reviewer | p. xxiii |
| Acknowledgments | p. xxv |
| Why Objective Caml (OCaml)? | p. 1 |
| Who Benefits from Learning OCaml? | p. 1 |
| What Is OCaml Good For? | p. 2 |
| Who Uses OCaml? | p. 3 |
| Where Did OCaml Come From? | p. 3 |
| What Is the Current State of the Art? | p. 4 |
| Why This Book? | p. 4 |
| What Is Covered? | p. 4 |
| Interacting with OCaml: The Toplevel (Chapter 2) | p. 4 |
| Syntax and Semantics (Chapter 3) | p. 5 |
| Understanding Functions (Chapter 4) | p. 5 |
| Practical: Creating a Simple Database (Chapter 5) | p. 5 |
| Primitive and Composite Types (Chapter 6) | p. 5 |
| Practical: Simple Database Reports, Exports, and Imports (Chapter 7) | p. 5 |
| Collections (Chapter 8) | p. 5 |
| Files and File I/O (Chapter 9) | p. 5 |
| Exception Handling (Chapter 10) | p. 6 |
| Practical: A URI Library (Chapter 11) | p. 6 |
| Using OCamldoc (Chapter 12) | p. 6 |
| Modules and Functors (Chapter 13) | p. 6 |
| Practical: A Spam Filter (Chapter 14) | p. 6 |
| Practical: A Network-Aware Scoring Function (Chapter 15) | p. 6 |
| Ocamllex and Ocamlyacc (Chapter 16) | p. 6 |
| Practical: Complex Log File Parsing (Chapter 17) | p. 6 |
| The Objective Part of Caml (Chapter 18) | p. 7 |
| Digression: OCaml Is Not Pure (Chapter 19) | p. 7 |
| Digression: Functional Programming (Chapter 20) | p. 7 |
| Practical: Web Programming (Chapter 21) | p. 7 |
| Practical: A Shoutcast Server (Chapter 22) | p. 7 |
| Using Threads (Chapter 23) | p. 7 |
| Practical: A Concurrent Web Crawler (Chapter 24) | p. 7 |
| Interfacing with OCaml (Chapter 25) | p. 7 |
| Practical: Time and Logging Libraries (Chapter 26) | p. 8 |
| Practical: Processing Binary Files (Chapter 27) | p. 8 |
| OCaml Development Tools (Chapter 28) | p. 8 |
| Camlp4 (Chapter 29) | p. 8 |
| Conclusion (Chapter 30) | p. 8 |
| What Isn't Covered? | p. 8 |
| What Are My Expectations for You? | p. 8 |
| Conclusion | p. 9 |
| Interacting with OCaml: The Toplevel | p. 11 |
| Distributions of OCaml | p. 12 |
| Official Distribution | p. 12 |
| Unofficial Releases | p. 12 |
| Installing OCaml | p. 12 |
| Windows | p. 12 |
| Linux | p. 14 |
| Interacting with the Toplevel | p. 16 |
| Hello World | p. 17 |
| Final Notes | p. 18 |
| Code Files | p. 18 |
| Basic Code Files | p. 19 |
| Custom Toplevel | p. 20 |
| Why Would You Do This? | p. 20 |
| How Do You Do This? | p. 20 |
| Conclusion | p. 20 |
| Syntax and Semantics | p. 21 |
| Variables in a Constant Language | p. 21 |
| What Variables Are Not | p. 21 |
| Let Bindings | p. 22 |
| Understanding Scope | p. 23 |
| Records and Types | p. 23 |
| Creating Enums and Simple User-Defined Types | p. 26 |
| Defining Records | p. 28 |
| More About Math | p. 29 |
| Integers and Floats | p. 29 |
| Others | p. 29 |
| Defining Functions | p. 30 |
| Imperative Programming | p. 30 |
| Recursion | p. 30 |
| Pattern Matching | p. 31 |
| Signatures | p. 32 |
| Conclusion | p. 32 |
| Understanding Functions | p. 33 |
| Creating Values and Functions | p. 33 |
| Functions Must Have One Return Type | p. 35 |
| Constraining Types in Function Calls | p. 35 |
| Using Higher-Order Functions | p. 36 |
| Using Lists | p. 37 |
| Anonymous Functions | p. 37 |
| Why Use Anonymous Functions? | p. 39 |
| Understanding Consequences of Functions As Data | p. 39 |
| Curried Functions | p. 41 |
| Working with the Distance Type | p. 41 |
| Creating Recursive Functions | p. 44 |
| Why Do Recursive Functions Need a Special Designation? | p. 45 |
| Tail Recursion and Efficient Programming | p. 45 |
| Doing More Pattern Matching | p. 46 |
| Understanding the Default Match | p. 47 |
| Bindings Within Pattern Matches | p. 47 |
| Guarded Matches: A Return to the Distance Calculator | p. 48 |
| Understanding Built-in Functions | p. 48 |
| Using Labels and Optional Arguments | p. 49 |
| Conclusion | p. 50 |
| Practical: Creating a Simple Database | p. 51 |
| Two Modules You Need | p. 51 |
| Using the Hashtbl Module in OCaml | p. 51 |
| Using the Marshal Module | p. 52 |
| How to Use These Modules | p. 52 |
| Working with Files | p. 52 |
| Accounts Database | p. 53 |
| Some Sample Data | p. 53 |
| Important Functions for Handling Data | p. 54 |
| Manipulating the Database Contents | p. 54 |
| Saving and Loading the Database | p. 56 |
| Interacting with the Database | p. 56 |
| Quick Note About Code Length | p. 59 |
| Getting Price Information | p. 59 |
| Conclusion | p. 60 |
| Primitive and Composite Types | p. 61 |
| Constant Type, Dynamic Data | p. 62 |
| Integers (Ints) | p. 62 |
| Floating-Point Numbers (Floats) | p. 63 |
| Strings and Chars | p. 64 |
| Using the Pervasives Module | p. 66 |
| Lists and Arrays | p. 66 |
| Exceptions | p. 66 |
| Other Types | p. 67 |
| Polymorphic Types | p. 67 |
| Composite Types | p. 68 |
| Polymorphic Variant Types | p. 69 |
| Conclusion | p. 71 |
| Practical: Simple Database Reports, Exports, and Imports | p. 73 |
| Format Codes | p. 73 |
| Printf | p. 74 |
| fprintf | p. 75 |
| eprintf | p. 75 |
| printf | p. 75 |
| sprintf | p. 75 |
| bprintf | p. 76 |
| kprintf | p. 76 |
| Scanf | p. 76 |
| Scanning Module | p. 76 |
| fscanf | p. 76 |
| scanf | p. 77 |
| sscanf | p. 77 |
| bscanf | p. 77 |
| kscanf | p. 77 |
| Why Use These Functions? | p. 77 |
| Why Not Regular Expressions? | p. 77 |
| What About Line-Oriented I/O? | p. 78 |
| The Right Tool for the Right Job | p. 78 |
| More About Buffers | p. 80 |
| Writing a Report | p. 80 |
| Writing Export Functions | p. 83 |
| Writing Import Functions | p. 84 |
| Generating Data | p. 86 |
| Conclusion | p. 87 |
| Collections | p. 89 |
| What Are Collections? | p. 90 |
| Comparison Functions | p. 90 |
| Lists | p. 91 |
| Arrays and Matrices | p. 97 |
| Hashtables | p. 100 |
| Queue | p. 103 |
| Stack | p. 105 |
| Set | p. 106 |
| Map | p. 109 |
| Not Quite Collections | p. 110 |
| Labeled Variants | p. 111 |
| Functors | p. 111 |
| Conclusion | p. 111 |
| Files and File I/O | p. 113 |
| Channels | p. 113 |
| Pervasives Module | p. 113 |
| Using Input Channels | p. 114 |
| Using Output Channels | p. 115 |
| Information About Files | p. 117 |
| Locking Files | p. 118 |
| Filenames and Portable Paths | p. 118 |
| Reading Directories | p. 119 |
| Large File Support | p. 120 |
| Sockets | p. 120 |
| Low-level Functions | p. 120 |
| High-level Functions | p. 122 |
| Unix and Windows | p. 122 |
| Conclusion | p. 122 |
| Exception Handling | p. 123 |
| Using Exceptions | p. 123 |
| Understanding Built-in Exceptions | p. 125 |
| Creating Custom Exceptions | p. 127 |
| Why Exceptions Cannot Be Polymorphic | p. 129 |
| Example: Some Error Functions | p. 130 |
| Can You Use an Object Instead? | p. 130 |
| Thou Shalt and Other Rules for Coding | p. 130 |
| Six Simple Rules | p. 131 |
| Using Asserts | p. 133 |
| Getting Line Numbers and Function Names | p. 134 |
| Conclusion | p. 134 |
| Practical: A URI Library | p. 135 |
| Looking at the URI Signature | p. 135 |
| Exceptions | p. 136 |
| Types | p. 136 |
| Functions | p. 137 |
| First Implementation | p. 137 |
| Using the Module | p. 141 |
| Improvements and Toys | p. 141 |
| Mythical Rot13 Path Maker | p. 141 |
| Using Regular Expressions | p. 142 |
| Conclusion | p. 144 |
| Using Ocamldoc | p. 145 |
| Who Uses Ocamldoc? | p. 145 |
| Using Ocamldoc | p. 146 |
| Running the Command | p. 146 |
| Markup | p. 148 |
| A Complete Example | p. 151 |
| Which Files to Document? | p. 153 |
| Creating Custom Tags and Generators | p. 153 |
| Conclusion | p. 154 |
| Modules and Functors | p. 155 |
| Using Modules | p. 155 |
| Defining Modules | p. 156 |
| Defined at the File Level | p. 156 |
| Using Interfaces | p. 158 |
| No Header Files in OCaml | p. 159 |
| Creating Multiple Views of a Module | p. 159 |
| What Are Generics? | p. 160 |
| Understanding Functors | p. 160 |
| When Should You Use Functors? | p. 162 |
| Currying Functors | p. 163 |
| A Complete Example | p. 163 |
| Dealing with Dependencies | p. 166 |
| Installing Modules | p. 167 |
| Library Directory | p. 167 |
| Compiling with Nonstandard Paths | p. 167 |
| Using OCamlMake | p. 167 |
| Using Findlib | p. 167 |
| Creating a Findlib META File | p. 167 |
| Conclusion | p. 168 |
| Practical: A Spam Filter | p. 169 |
| Naive Bayesian Spam Filtration | p. 169 |
| Brief Digression | p. 169 |
| Talking About the Design | p. 170 |
| Code | p. 171 |
| Compiling the Code | p. 174 |
| Running It | p. 175 |
| Things You Might Want | p. 176 |
| Deficiencies in the Code | p. 178 |
| Conclusion | p. 178 |
| Practical: A Network-Aware Scoring Function | p. 179 |
| Supporting Clients and Servers | p. 179 |
| Creating a Simple Server | p. 179 |
| Implementing a Spam Server | p. 182 |
| Providing Client Functions | p. 189 |
| Conclusion | p. 191 |
| Ocamllex and Ocamlyacc | p. 193 |
| Lexing Has No Relation to Luthor | p. 193 |
| Why Use a Lexer Generator? | p. 194 |
| Using Ocamllex | p. 196 |
| Know Your Grammar | p. 202 |
| Why Use a Parser? | p. 203 |
| Using Ocamlyacc | p. 203 |
| Using Ocamlyacc and Ocamllex | p. 207 |
| A More Complicated Example | p. 208 |
| BNF and EBNF | p. 210 |
| Conclusion | p. 211 |
| Practical: Complex Log File Parsing | p. 213 |
| A Simple Example | p. 213 |
| Sample Data | p. 213 |
| Code | p. 214 |
| A Complex Example | p. 217 |
| Example Log File with Various Kinds of Entries | p. 217 |
| Code | p. 217 |
| Discussion | p. 222 |
| Conclusion | p. 223 |
| The Objective Part of Caml | p. 225 |
| Basics | p. 225 |
| Simple Example | p. 226 |
| Why Use Classes and Objects? | p. 227 |
| Object and Class Keywords | p. 228 |
| Methods May Not Be Polymorphic | p. 229 |
| Direct Objects | p. 231 |
| Initializers and Finalizers | p. 232 |
| Privacy and Data Member Access | p. 233 |
| Internal Classes | p. 233 |
| Virtual Classes and Methods | p. 234 |
| Parameterized Objects | p. 234 |
| Constraints | p. 234 |
| Inheritance | p. 236 |
| Simple Inherit | p. 236 |
| Parametric Inherit | p. 237 |
| Composition vs. Inheritance | p. 238 |
| Multiple Inheritance | p. 239 |
| Functional Objects and Object Cloning | p. 241 |
| Larger Example | p. 243 |
| Conclusion | p. 248 |
| Digression: OCaml Is Not Pure | p. 249 |
| Functional Programming | p. 251 |
| Really, Why Should You Care? | p. 251 |
| Purely Functional Data Structures | p. 252 |
| Languages Like OCaml | p. 253 |
| Languages Like Haskell | p. 253 |
| Benefits of Impurity | p. 254 |
| Hiding the Impure | p. 254 |
| Preventing Information Leaks | p. 254 |
| Conclusion | p. 260 |
| Digression: Functional Programming | p. 261 |
| Overview of Programming Styles | p. 261 |
| Structured Programming | p. 262 |
| Object-Oriented Programming | p. 262 |
| Functional Programming | p. 263 |
| Advantages of Functional Programming | p. 266 |
| Less Code | p. 267 |
| Fewer Bugs | p. 268 |
| Programming in the Large | p. 270 |
| Correctness of Programs | p. 271 |
| Concurrency | p. 271 |
| Reasoning About Programs | p. 272 |
| Conclusion | p. 272 |
| Practical: Web Programming | p. 273 |
| What Does Web Programming Mean? | p. 273 |
| CGI | p. 274 |
| FastCGI | p. 275 |
| Integrated Approaches | p. 275 |
| Chapter Focus | p. 276 |
| CGI | p. 276 |
| Ocamlnet | p. 276 |
| Mod_caml | p. 276 |
| Rolling Your Own CGI Functions | p. 277 |
| Benefits | p. 277 |
| Drawbacks | p. 278 |
| Longer Examples | p. 278 |
| Ocamlnet | p. 284 |
| Mod_caml Library | p. 289 |
| Examples | p. 289 |
| If You Are Not Using Apache | p. 291 |
| Conclusion | p. 291 |
| Practical: A Shoutcast Server | p. 293 |
| Shoutcast Protocol | p. 293 |
| Parsing MP3 Files | p. 295 |
| Binary Files | p. 295 |
| Getting the ID3 Tag | p. 295 |
| Server Framework | p. 300 |
| Using the High-Level Connection Functions | p. 305 |
| Implementing the Shoutcast Protocol | p. 305 |
| Connecting to Your Server | p. 307 |
| Conclusion | p. 308 |
| Using Threads | p. 309 |
| Why Do You Need Concurrency? | p. 309 |
| What Kinds of Concurrency Does OCaml Support? | p. 310 |
| Creating and Using a Thread | p. 310 |
| Details About the Modules | p. 316 |
| Thread | p. 316 |
| Mutex | p. 317 |
| Condition | p. 318 |
| Event | p. 319 |
| Some Code from the Last Chapter | p. 322 |
| A More Complex Example | p. 322 |
| Conclusion | p. 327 |
| Practical: A Concurrent Web Crawler | p. 329 |
| Deceptively Simple Application | p. 329 |
| Design Goals | p. 330 |
| Regular Expressions | p. 330 |
| Understanding the Code | p. 332 |
| Building and Running the Code | p. 344 |
| Generating a Graph of a Site | p. 347 |
| Conclusion | p. 348 |
| Interfacing with OCaml | p. 349 |
| Foreign Function Interface | p. 349 |
| Implementing Your Own Primitives | p. 350 |
| Accessing Value Elements | p. 351 |
| Allocation and the Garbage Collector | p. 352 |
| Exceptions | p. 352 |
| Defining Functions | p. 352 |
| Using a Tool | p. 355 |
| IDL and camlidl | p. 355 |
| Understanding Linking Options | p. 356 |
| Readline Example | p. 357 |
| Other Tools | p. 358 |
| Conclusion | p. 358 |
| Practical: Time and Logging Libraries | p. 359 |
| Time Library | p. 359 |
| Time Library via IDL | p. 360 |
| Time Library via CAMLprim | p. 362 |
| Logging Library | p. 365 |
| Make a META File for this Library | p. 371 |
| Conclusion | p. 374 |
| Processing Binary Files | p. 375 |
| Endianness | p. 375 |
| Support for Binary Files and Data | p. 376 |
| First Example | p. 376 |
| Finding Matches Between Binary Files | p. 380 |
| Reading Bitmaps | p. 383 |
| Conway's Game of Life | p. 390 |
| Graphics in OCaml | p. 399 |
| Conclusion | p. 399 |
| OCaml Development Tools | p. 401 |
| Build Tools | p. 401 |
| OCaml Compiler | p. 405 |
| File Extensions Used | p. 408 |
| Findlib | p. 409 |
| Conclusion | p. 410 |
| Camlp4 | p. 411 |
| Revised Syntax | p. 411 |
| What Is Camlp4? | p. 412 |
| Streams and Parsers | p. 413 |
| Understanding Streams | p. 413 |
| Understanding Parsers | p. 415 |
| Example Configuration File Parser | p. 415 |
| Domain-Specific Languages (DSLs) | p. 419 |
| Extending OCaml | p. 420 |
| More About Quotations and Levels | p. 421 |
| A Longer Example | p. 421 |
| Complex Example | p. 423 |
| Conclusion | p. 429 |
| Conclusion | p. 431 |
| What This Book Covered | p. 431 |
| ocamllex and ocamlyacc | p. 432 |
| Camlp4 and Stream Parsers | p. 432 |
| Where to Go from Here | p. 433 |
| More Functional Programming | p. 433 |
| Camlp4 | p. 433 |
| Resources | p. 433 |
| Mailing Lists | p. 433 |
| Other Resources | p. 434 |
| Thank You | p. 434 |
| Final Example | p. 434 |
| Index | p. 445 |
| Table of Contents provided by Ingram. All Rights Reserved. |
ISBN: 9781590596203
ISBN-10: 159059620X
Series: Apress Ser.
Published: 1st October 2006
Format: Hardcover
Language: English
Number of Pages: 488
Audience: General Adult
Publisher: Springer Nature B.V.
Country of Publication: US
Dimensions (cm): 23.5 x 18.42 x 3.18
Weight (kg): 0.91
Shipping
| Standard Shipping | Express Shipping | |
|---|---|---|
| Metro postcodes: | $9.99 | $14.95 |
| Regional postcodes: | $9.99 | $14.95 |
| Rural postcodes: | $9.99 | $14.95 |
Orders over $79.00 qualify for free shipping.
How to return your order
At Booktopia, we offer hassle-free returns in accordance with our returns policy. If you wish to return an item, please get in touch with Booktopia Customer Care.
Additional postage charges may be applicable.
Defective items
If there is a problem with any of the items received for your order then the Booktopia Customer Care team is ready to assist you.
For more info please visit our Help Centre.

























