Introduction to Objective Caml

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Author(s): Hickey J.
Edition: web draft
Year: 2008

Language: English
Pages: 284

Preface......Page 9
Introduction......Page 11
Additional Sources of Information......Page 13
Basic expressions......Page 15
int: the integers......Page 16
float: the floating-point numbers......Page 17
string: character strings......Page 18
bool: the Boolean values......Page 19
Operator precedences......Page 20
The OCaml type system......Page 21
Compiling your code......Page 22
Exercises......Page 24
Variables and Functions......Page 25
Functions......Page 26
Recursive functions......Page 28
Higher order functions......Page 29
Variable names......Page 30
Labeled parameters and arguments......Page 31
Rules of thumb......Page 32
Exercises......Page 34
Basic pattern matching......Page 39
Functions with matching......Page 40
Values of other types......Page 41
Incomplete matches......Page 43
Patterns are everywhere......Page 44
Exercises......Page 45
Polymorphism......Page 47
Value restriction......Page 48
Other kinds of polymorphism......Page 49
Tuples......Page 51
Lists......Page 52
Optimization of tail-recursion......Page 54
Lists and tail recursion......Page 55
Exercises......Page 57
Unions......Page 59
Unbalanced binary trees......Page 61
Unbalanced, ordered, binary trees......Page 62
Balanced red-black trees......Page 63
Open union types (polymorphic variants)......Page 65
Type definitions for open types......Page 66
Some common built-in unions......Page 67
Exercises......Page 68
Reference cells and side-effects......Page 71
Pure functional programming......Page 72
Queues......Page 74
Doubly-linked lists......Page 75
Memoization......Page 77
Graphs......Page 79
Exercises......Page 83
Records......Page 87
Functional and imperative record updates......Page 88
Field label namespace......Page 89
Strings......Page 90
Hash tables......Page 91
Exercises......Page 94
Exceptions......Page 97
Nested exception handlers......Page 99
Invalid_argument and Failure......Page 100
Pattern matching failure......Page 101
Memory exhaustion exceptions......Page 102
Break statements......Page 103
Unwind-protect (finally)......Page 104
The exn type......Page 105
Exercises......Page 106
File opening and closing......Page 109
Writing and reading values on a channel......Page 110
Channel manipulation......Page 111
String buffers......Page 112
Formatted output with Printf......Page 113
Formatted input with Scanf......Page 115
Exercises......Page 116
Where is the main function?......Page 119
Multiple files and abstraction......Page 121
Defining an interface......Page 122
Transparent type definitions......Page 124
Interface errors......Page 125
Using open to expose a namespace......Page 127
Debugging a program......Page 128
Exercises......Page 131
Structures and signatures......Page 135
Module definitions......Page 137
The let module expression......Page 138
Recursive modules......Page 139
Using include to extend implementations......Page 140
Abstraction, friends, and module hiding......Page 142
Using include with incompatible signatures......Page 143
Sharing constraints......Page 144
Exercises......Page 146
Functors......Page 149
Module sharing constraints......Page 151
Module re-use using functors......Page 153
Recursive modules and functors......Page 155
A complete example......Page 156
Exercises......Page 160
Objects......Page 165
Encapsulation and polymorphism......Page 166
Basis transformations......Page 168
Functional update......Page 169
Object factories......Page 170
Imperative objects......Page 171
self: referring to the current object......Page 173
Initializers; private methods......Page 174
Object types, coercions, and subtyping......Page 175
Coercions......Page 176
Subtyping......Page 178
Narrowing......Page 180
Why narrowing is bad......Page 181
Alternatives to objects......Page 182
Exercises......Page 184
Class basics......Page 189
Class types......Page 190
Classes with let-expressions......Page 191
Type inference......Page 192
Inheritance......Page 193
Method override......Page 195
Class types......Page 196
Class type constraints and hiding......Page 197
Classes and class types as object types......Page 199
Inheritance is not subtyping......Page 200
Modules and classes......Page 203
Polymorphic methods and virtual classes......Page 205
Polymorphic methods......Page 206
Virtual (abstract) classes and methods......Page 208
Stacks......Page 209
Lists and stacks......Page 210
Exercises......Page 212
Examples of multiple inheritance......Page 219
Inheriting from multiple virtual classes......Page 220
Mixins......Page 221
Overriding and shadowing......Page 223
Repeated inheritance......Page 224
Is-a vs. has-a......Page 226
Mixins revisited......Page 228
Exercises......Page 229
Polymorphic dictionaries......Page 231
Instantiating a polymorphic class......Page 232
Inheriting from a polymorphic class......Page 233
Polymorphic class types......Page 234
Coercing polymorphic classes......Page 236
Variance annotations......Page 238
Positive and negative occurrences......Page 240
Coercing by hiding......Page 241
Type constraints......Page 242
Late binding......Page 244
Extending the definitions......Page 246
Exercises......Page 252
Notation......Page 261
Terminal symbols (lexemes)......Page 262
Keywords......Page 263
Integer literals......Page 264
String literals......Page 265
Simple names......Page 266
Path names......Page 267
Expressions......Page 269
Precedence of operators......Page 271
Type expressions......Page 273
Type definitions......Page 274
Structure items and module expressions......Page 275
Signature items and module types......Page 276
Class expressions and types......Page 277
Class types......Page 278