This fast-moving tutorial introduces you to OCaml, an industrial-strength programming language designed for expressiveness, safety, and speed. Through the book’s many examples, you’ll quickly learn how OCaml stands out as a tool for writing fast, succinct, and readable systems code. Real World OCaml takes you through the concepts of the language at a brisk pace, and then helps you explore the tools and techniques that make OCaml an effective and practical tool. In the book’s third section, you’ll delve deep into the details of the compiler toolchain and OCaml’s simple and efficient runtime system.
Learn the foundations of the language, such as higher-order functions, algebraic data types, and modules
Explore advanced features such as functors, first-class modules, and objects
Leverage Core, a comprehensive general-purpose standard library for OCaml
Design effective and reusable libraries, making the most of OCaml’s approach to abstraction and modularity
Tackle practical programming problems from command-line parsing to asynchronous network programming
Examine profiling and interactive debugging techniques with tools such as GNU gdb Language ConceptsChapter 1 : A Guided Tour OCaml as a CalculatorFunctions and Type InferenceTuples, Lists, Options, and Pattern MatchingRecords and VariantsImperative ProgrammingA Complete ProgramWhere to Go from HereChapter 2 : Variables and Functions VariablesFunctionsChapter 3 : Lists and Patterns List BasicsUsing Patterns to Extract Data from a ListLimitations (and Blessings) of Pattern MatchingUsing the List Module EffectivelyTail RecursionTerser and Faster PatternsChapter 4 : Files, Modules, and Programs Single-File ProgramsMultifile Programs and ModulesSignatures and Abstract TypesConcrete Types in SignaturesNested ModulesOpening ModulesIncluding ModulesCommon Errors with ModulesDesigning with ModulesChapter 5 : Records Patterns and ExhaustivenessField PunningReusing Field NamesFunctional UpdatesMutable FieldsFirst-Class FieldsChapter 6 : Variants Catch-All Cases and RefactoringCombining Records and VariantsVariants and Recursive Data StructuresPolymorphic VariantsChapter 7 : Error Handling Error-Aware Return TypesExceptionsChoosing an Error-Handling StrategyChapter 8 : Imperative Programming Example: Imperative DictionariesPrimitive Mutable Datafor and while LoopsExample: Doubly Linked ListsLaziness and Other Benign EffectsInput and OutputOrder of EvaluationSide Effects and Weak PolymorphismSummaryChapter 9 : Functors A Trivial ExampleA Bigger Example: Computing with IntervalsExtending ModulesChapter 10 : First-Class Modules Working with First-Class ModulesExample: A Query-Handling FrameworkLiving Without First-Class ModulesChapter 11 : Objects OCaml ObjectsObject PolymorphismImmutable ObjectsWhen to Use ObjectsSubtypingChapter 12 : Classes OCaml ClassesClass Parameters and PolymorphismObject Types as InterfacesInheritanceClass TypesOpen RecursionPrivate MethodsBinary MethodsVirtual Classes and MethodsInitializersMultiple InheritanceTools and TechniquesChapter 13 : Maps and Hash Tables MapsHash TablesChoosing Between Maps and Hash TablesChapter 14 : Command-Line Parsing Basic Command-Line ParsingArgument TypesAdding Labeled Flags to the Command LineGrouping Subcommands TogetherAdvanced Control over ParsingCommand-Line Autocompletion with bashAlternative Command-Line ParsersChapter 15 : Handling JSON Data JSON BasicsParsing JSON with YojsonSelecting Values from JSON StructuresConstructing JSON ValuesUsing Nonstandard JSON ExtensionsAutomatically Mapping JSON to OCaml TypesChapter 16 : Parsing with OCamllex and Menhir Lexing and ParsingDefining a ParserDefining a LexerBringing It All TogetherChapter 17 : Data Serialization with S-Expressions Basic UsageThe Sexp FormatPreserving InvariantsGetting Good Error MessagesSexp-Conversion DirectivesChapter 18 : Concurrent Programming with Async Async BasicsExamples: An Echo ServerExample: Searching Definitions with DuckDuckGoException HandlingTimeouts, Cancellation, and ChoicesWorking with System ThreadsThe Runtime SystemChapter 19 : Foreign Function Interface Example: A Terminal InterfaceBasic Scalar C TypesPointers and ArraysStructs and UnionsPassing Functions to CLearning More About C BindingsChapter 20 : Memory Representation of Values OCaml Blocks and ValuesBlocks and ValuesTuples, Records, and ArraysVariants and ListsPolymorphic VariantsString ValuesCustom Heap BlocksChapter 21 : Understanding the Garbage Collector Mark and Sweep Garbage CollectionGenerational Garbage CollectionThe Fast Minor HeapThe Long-Lived Major HeapAttaching Finalizer Functions to ValuesChapter 22 : The Compiler Frontend: Parsing and Type Checking An Overview of the ToolchainParsing Source CodePreprocessing Source CodeStatic Type CheckingThe Typed Syntax TreeChapter 23 : The Compiler Backend: Bytecode and Native code The Untyped Lambda FormGenerating Portable BytecodeCompiling Fast Native CodeSummarizing the File ExtensionsColophon