Skip to main content

2007 | Buch

Practical OCaml

insite
SUCHEN

Über dieses Buch

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.

Inhaltsverzeichnis

Frontmatter
Chapter 1. Why Objective Caml (OCaml)?
Abstract
It’s a fair question. You have picked up this book, so I assume that you are (at a minimum) interested in Objective Caml (OCaml). That interest is enough for some to cover the “why” question. If you write programs to make a living, perhaps the glib, navel-gazing answer is not what you were looking for.
Chapter 2. Interacting with OCaml: The Toplevel
Abstract
The OCaml toplevel, the interactive OCaml interpreter, is one of the many powerful features of OCaml that can help you be more productive. This interactive interpreter enables you to enter OCaml code and have it evaluated immediately. You can then prototype code on-the-fly (much like Python, which has a similar system) instead of relying on the compile-run-debug cycle found in languages such as Java or C. The toplevel provides an interactive read/eval/print loop and gives you access to all features of the language.
Chapter 3. Syntax and Semantics
Abstract
So, now that you know how to install and start the OCaml toplevel, you will learn how to actually do things in the language.
Chapter 4. Understanding Functions
Abstract
Functions are the main part of OCaml, which is not surprising because OCaml is a functional programming (FP) language. Functions in OCaml have signatures that are displayed in the OCaml toplevel and are used extensively in the module language.
Chapter 5. Practical: Creating a Simple Database
Abstract
Now that you have a basic overview of the OCaml language under your belt, it’s time to actually write code that does something. In this chapter, you will write a small database of securities trades and accounts. You will be able to add and remove (buy and sell) quantities of a given stock. There is also a simple network-based client to retrieve current price information from the web.
Chapter 6. Primitive and Composite Types
Abstract
Chapter 3 introduced you to the OCaml concept of type; here you will learn more about types—their importance and things you can do with and about them. This chapter discusses the primitive types in OCaml (they are sometimes referred to as basic types; both terms are used interchangeably here).
Chapter 7. Practical: Simple Database Reports, Exports, and Imports
Abstract
Now that you have a solid grounding in the OCaml types and functions, you will revisit the simple database from Chapter 3 and Chapter 5. One of the major things missing from that database was any way to import or export data. Sure, you can add and remove items, but if you really want to use this database, you will want more functionality.
Chapter 8. Collections
Abstract
OCaml has a standard library that provides for several collections to handle everything from lists to sets. Although not all these collections are purely functional, the purity of a given container isn’t all that important to many programmers.
Chapter 9. Files and File I/O
Abstract
Although pure algorithmic programming can certainly be fun, any programming language would not be very practical without the capability to store and retrieve data from a persistent source. OCaml is a practical language that provides sophisticated methods for storing, retrieving, sending, and receiving data from outside itself.
Chapter 10. Exception Handling
Abstract
OCaml has exceptions, which are integrated into the language and are basic types. Exceptions cannot be polymorphic. Unlike some languages (such as Java), there is only one kind of exception, and there is no requirement to handle any exception. However, it’s often a good idea. Also, exception handling in OCaml is quite fast.
Chapter 11. Practical: A URI Library
Abstract
OCaml provides a module for dealing with paths and filenames in an operating system (OS)-independent manner (this applies only to files, not to Uniform Resource Identifiers [URIs]). I have had to convert programs from using files to using other sources of input; during one of those conversions, I wondered how nice it would be if OCaml had functions that worked with URIs such as Java. There is a very comprehensive URI module included in the Netstring library. It is a third-party library that is part of the Ocamlnet package. Written by Gerd Stoplmann, this library can be found at http://www.ocaml-programming.de.
Chapter 12. Using Ocamldoc
Abstract
Almost everyone can agree that writing proper documentation is very important for programmers, but that this task is often overlooked or done poorly.
Chapter 13. Modules and Functors
Abstract
OCaml provides a very advanced module system. It is based on the meta-language (ML) module system, and it is similar to systems provided in Modula-3 and Ada as well. The module system is basically a small typed language on top of OCaml that enables a programmer to group values, functions, types, classes, and other modules together.
Chapter 14. Practical: A Spam Filter
Abstract
Everyone knows what spam is, even if they don’t know that it refers to unwanted (and usually advertisement-ridden) email instead of the venerable meat product made by the Hormel Corporation.
Chapter 15. Practical: A Network-Aware Scoring Function
Abstract
In the last Chapter, you built a simple spam detector (well, a simple text classifier that can be used for spam detection). It used an on-disk storage mechanism, which works fine for one person on one machine. However, what if that person uses multiple machines? What if you want to have many people or processes contributing information to the text storage?
Chapter 16. Ocamllex and Ocamlyacc
Abstract
You might have heard of Lex and Yacc. Lex, the Lexical Analyzer Generator, is a tool that helps you build programs that are built around regular expressions. Yacc, Yet Another Compiler Compiler, is a program for building interpreters and compilers from grammars that you define. Although you do not have to use these tools together (people often use just Lex or Yacc with a custom lexer), but they are often used together.
Chapter 17. Practical: Complex Log File Parsing
Abstract
Ocamllex and ocamlyacc can be used to build compilers for languages. They also can be used to handle any kind of text file that you might want to parse. Complex log files are places in which ocamllex and ocamlyacc can be used that are not often talked about in the literature.
Chapter 18. The Objective Part of Caml
Abstract
Objective Caml includes very powerful object-oriented features. Although that might seem somewhat obvious given that “objective” is in the language name, the extent and focus of the object system in OCaml is different from many other object-oriented languages currently in use.
Chapter 19. Digression: OCaml Is Not Pure
Abstract
Apurely functional function has no side effects. A purely functional programming language would not allow functions with side effects to be defined. But wait, there’s more! Because functions and algorithms cannot have side effects, variables are immutable and persistent. This persistence is not the same as disk storage or serialization; it means that previous versions of a given value can be retained by the language.
Chapter 20. Digression: Functional Programming
Abstract
At various points in this book, I noted that OCaml is a functional programming language. Chapter 19 told you that OCaml is not a purely functional programming language and what that lack of purity is about.
Chapter 21. Practical: Web Programming
Abstract
Web programming is one of the best ways for a language to gain visibility. Ever since the web gained widespread popularity, developing for web targets has been important for a wide variety of programmers.
Chapter 22. Practical: A Shoutcast Server
Abstract
Shoutcast is a product from Nullsoft (which is also the creator of the WinAMP MP3 player for Windows) to enable audio broadcasting over a network. Nullsoft’s Shoutcast server enables clients to stream MP3 data from one client to a server, which is the server that clients connect to in order to listen to that data stream. It is, in effect, a multiplexer of audio streams. Shoutcast was one of the first applications of its kind to become popular. Although streaming network audio predates Shoutcast, it was Nullsoft’s product that had the power and flexibility that enabled it to take off.
Chapter 23. Using Threads
Abstract
As you are reading this, you are performing multiple tasks simultaneously. You are thinking about the words on the page, moving your eyes, hearing sounds in your environment, interpreting these sounds, and more. This ability to perform concurrent actions makes things like walking, talking, and pretty much everything else a living being does possible. Most computer programs, however, do not have the capability to perform concurrent actions.
Chapter 24. Practical: A Concurrent Web Crawler
Abstract
This chapter introduces a concurrent web crawler written in OCaml, which traverses a web server and finds all the local href links. It then outputs information about which pages link together. A web crawler is different from a web browser in that the web crawler is automated. Both are web (or HTTP) clients and are quite similar, but this automation versus interactivity is the important distinction.
Chapter 25. Interfacing with OCaml
Abstract
No language is an island. There are situations in which a programmer wants to interface code with existing code, or system calls, or a new library that is not written in OCaml. Sometimes this can be accomplished using sockets or some other Remote Procedure Call (RPC) mechanism. Sometimes, though, she will need to interface her OCaml code with foreign functions.
Chapter 26. Practical: Time and Logging Libraries
Abstract
The OCaml standard library is big, but it still could be more complete. Two areas in which it is lacking include time functions and logging. If you are used to a programming language (such as Java or Python) that has an absolutely huge standard library that includes everything you could ever want in the world, you might be disappointed by OCaml’s standard library.
Chapter 27. Processing Binary Files
Abstract
All data is not text. Binary data is used in all kinds of applications. Often, accessing binary data is faster than text data, and it is space-efficient, too. Until now we have not talked about what you need to do differently to process binary data in OCaml.
Chapter 28. OCaml Development Tools
Abstract
Having a programming language is not the only thing you need to actually do development in that language. You also need tools that make the process of developing and debugging code easier.
Chapter 29. Camlp4
Abstract
Camlp4, which provides a very interesting and powerful way to extend and change the OCaml language, enables the easy definition of domain-specific languages (DSLs) based on OCaml. The name Camlp4 refers to the fact that it is a preprocessor and pretty printer (four Ps) for OCaml.
Chapter 30. Conclusion
Abstract
You have now arrived at the last chapter of this book. Although this book does not cover all aspects of OCaml programming, I have tried to present information that can help you become an OCaml programmer if you are not one yet—or a better OCaml programmer if you are.
Backmatter
Metadaten
Titel
Practical OCaml
verfasst von
Joshua B. Smith
Copyright-Jahr
2007
Verlag
Apress
Electronic ISBN
978-1-4302-0244-8
Print ISBN
978-1-59059-620-3
DOI
https://doi.org/10.1007/978-1-4302-0244-8