714 search results for ""
-
Basic Data Types and Pattern Matching
Characters
The module Uchar provides support for Unicode characters. Operations on char values are provided by the Stdlib and the Char modules. Values of type char correspond to the 256 symbols of
Introduction -
Sequences
Consumer Example: Seq.iter
In print_seq , Seq.iter takes the function print_int and applies it to each element as they are generated. If List.iter was used, the whole integer list would be needed before displaying the
Data Structures -
Options
Bind an Option
This example retrieves a configuration value and ensures it falls within a valid range. Example 3 : Chaining Computations Notice how the function user_to_email does not require explicit pattern
Data Structures -
Error Handling
Language Bugs
Here is an example of such a bug: <https://github.com/ocaml/ocaml/issues/7241> Make sure the crash affects both compilers: bytecode and native Write a self-contained and minimal proof-of-concept co
Guides -
Objects
A Note About self
The reference to self names the object, allowing you to call methods in the same class or pass the object to functions outside the class. In other words, it's exactly the same as this in C++
Advanced Topics -
Arrays
Folding an Array
These functions derive a single value from the whole array. For example, they can be used to find the maximum element of an array: fold_right f a init computes f a.(0) (f a.(1) ( ... (f a.(n-1)
Data Structures -
Sequences
Producer Example: Seq.unfold
This application of Seq.unfold has type unit -> int Seq.node , making it a function, a deferred producer. Each time this function is called, a new element is produced.
Data Structures -
A Tour of OCaml
Functions
Some functions, such as String.ends_with have labelled parameters. Labels are useful when a function has several parameters of the same type; naming arguments allows to guess their purpose. Above,
First Steps -
Maps
Maps With Custom Key Types
Note that our module has a type t and also a compare function. Now we can call the Map.Make functor to get a map for non-negative numbers: We'll start by defining a module for strings th
Data Structures -
Sequences
Fibs with Seq.cons
This produces a never-ending recursion that leads to a stack overflow. <!-- Or with an int version: ```ocaml # let rec ints_v1 n = Seq.cons n (n + 1);; val fibs : int -> int -> int Seq.t = <fun> #
Data Structures -
OCaml Programming Guidelines
Naming Complex Arguments
<!-- $MDX skip --> write <!-- $MDX skip --> In place of
Resources -
Modules
Submodule Implementation
dune Definitions from a submodule are accessed by chaining module names, here Florence.Hello.print . Here is the updated dune file, with an additional executable: glasgow.ml florence.ml
Module System -
Loops and Recursions
Recursion
In the first example, we'll read the whole file into memory (into a long string). There are essentially three possible approaches to this: Writing recursive functions requires a change in mindse
Introduction -
Maps
Finding Entries in a Map
Note that find_first_opt and find_last_opt return the key-value pair, not just the value. The functions find_first and find_last behave similarly, except they throw exceptions instead of
Data Structures -
Options
The Standard Library Option Module
Most of the functions in this section, as well as other useful ones, are provided by the OCaml standard library in the Stdlib.Option module.
Data Structures -
Error Handling
Exceptions
Here, we add a variant Foo to the type exn and create a function that will raise this exception. Now, how do we handle exceptions? The construct is try ... with ... : Exceptions belong to
Guides -
Using OCaml with GitHub Actions
setup-ocaml
setup-ocaml is an action that provides an OS-neutral interface to opam , and so will not add features that only work on one operating system. It is the de-facto standard for OCaml CI workflows. I
Additional Tooling -
Basic Data Types and Pattern Matching
Results
Operations on results are provided by the Result module. Results are discussed in the Error Handling guide. The result type can be used to express that a function's outcome can be either succ
Introduction -
File Manipulation
Gotchas
Don't forget to flush your out_channel s if you want to actually write something. This is particularly important if you are writing to non-files such as the standard output ( stdout ) or a socke
Tutorials -
OCaml on Windows
Vim and Emacs
If you use Vim , the default Cygwin Vim will not work with Merlin. You will need to install Vim separately. In addition to the usual instructions printed when installing Merlin, you may need to
Resources -
OCaml-CI
Step 1: Install the GitHub App
Add the app to your account by selecting Configure at https://github.com/apps/ocaml-ci , then select the GitHub account or organisation and click Configure .
Additional Tooling -
Mutability and Imperative Control Flow
Arrays
For a more detailed discussion on arrays, see the Arrays tutorial. the array location to update (when on the left-hand side of <- ), or the cell's content (when on the right-hand side of <- ). T
Introduction -
File Manipulation
Buffered Channels
channels that write to a file: type out_channel channels that read from a file: type in_channel The normal way of opening a file in OCaml returns a channel . There are two kinds of channels:
Tutorials -
Sequences
Consumers vs Producers
A function with a sequence parameter consumes it; it's a sequence consumer. A function with a sequence result produces it; it's a sequence producer. In both cases, consumption and production occurs o
Data Structures -
First-Class Modules
Basic Example
The expression (module SimplePrinter : Printer) converts a module into a value of type (module Printer) . The expression (val printer : Printer) converts it back. The type annotation can often b
Module System -
Arrays
Modifying Array Elements
Note that this operation returns unit , not the modified array. even_numbers is modified in place as a side effect. To modify an element in an array, we simply assign a new value to it using th
Data Structures -
Debugging
Setting Break Points
Now we can guess why List.assoc will fail to find "INRIA" in the list... Then, we can step and find what happens just before ( <|b|> ) List.assoc is about to be called in find_address : L
Guides -
Configuring Your Editor
Windows Users
If you used the DkML distribution, you will need to: 1. Go to File > Preferences > Settings view (or press Ctrl , ) 2. Select User > Extensions > OCaml Platform 3. Uncheck OCaml: U
Tooling -
Your First OCaml Program
Conclusion
<!-- TODO: link Project Quickstart If you're already familiar with lists, maps, and folds, and need to be productive as fast as possible, dive into the “Project Quickstart” guide. --> This tuto
First Steps -
Modules
Submodule With Signatures
The first version made Florence.Hello.message public. In this version it can't be accessed from glasgow.ml . To define a submodule's interface, we can provide a module signature . This is do
Module System -
OCaml-CI
How It Works
This approach makes rebuilds fast because Docker reuses cached steps when opam files haven't changed. Discovers installations of the GitHub app Lists repositories to check for each installation Id
Additional Tooling -
Arrays
Accessing Array Elements
You can access individual elements of an array using the .(index) syntax, with the index of the element you want to access. The index of the first element is 0, and the index of the last element
Data Structures -
Basic Data Types and Pattern Matching
Options
Operations on options are provided by the Option module. Options are discussed in the Error Handling guide. Here is an example of pattern matching on an option value: The option type is a
Introduction -
Installing OCaml
For Linux
Note : The Debian package for opam, which is also used in Ubuntu, has the OCaml compiler as a recommended dependency. By default, such dependencies are installed. If you want to only install opam w
First Steps -
Values and Functions
The Pipe Operator
This is just like a Unix shell pipe. The pipe operator ( |> ) also avoids parentheses but in reversed order: function on right, argument on left.
Introduction -
OCaml Programming Guidelines
Delimiters
A space should always follow a delimiter symbol, and spaces should surround operator symbols. It has been a great step forward in typography to separate words by spaces in order to make written t
Resources -
A Tour of OCaml
Anonymous Functions
We can write anonymous functions and immediately apply them to a value: Anonymous functions do not have a name, and they are defined with the fun keyword:
First Steps -
Basic Data Types and Pattern Matching
Type Aliases
This is mostly useful as a means of documentation or to shorten long type expressions. Just like values, any type can be given a name.
Introduction -
Installing OCaml
Join the Community
Make sure you join the OCaml community . You'll find many community members on Discuss or Discord . These are great places to ask for help if you have any issues.
First Steps -
Operators
Unary Operators
This allows users to write more compact code. However, be careful not to write excessively terse code, as it is harder to maintain. Understanding operators must be obvious to most readers, otherwise
Advanced Topics -
Lists
Association Lists
When using association lists, and for other purposes, it is sometimes useful to be able to make a list of pairs from a pair of lists and vice versa. The List module provides the functions sp
Introduction -
The Compiler Backend: Bytecode and Native code
Perf
<div class="note"> Perf has a growing collection of other commands that let you archive these runs and compare them against each other. You can read more on the home page . This trace broadly
Runtime & Compiler -
Arrays
Creating Arrays
Array.init generates an array of a given length by applying a function to each index of the array, starting at 0. The following line of code creates an array containing the first 5 even numbers us
Data Structures -
OCaml-CI
Project Requirements
OCaml-CI will automatically determine which OCaml versions and platforms to test based on your opam file constraints. Valid .opam files describing your package dependencies A dune-project or du
Additional Tooling -
Your First OCaml Program
Watch Mode
Before we dive in, note that you will typically want to use Dune's watch mode to continually compile and optionally restart your program. This ensures that the language server has the freshest poss
First Steps -
Error Handling
Inherently Unsafe Functions
Some OCaml functions are inherently unsafe. Use them with care, not like this:
Guides -
OCaml-CI
Step 2: Select Repositories
Select only the repositories you want tested (starting with no more than three please). If you select All Repositories , we won't be able to build anything!
Additional Tooling -
Introduction to opam Switches
Listing Your Switches
The arrow -> marks the currently active switch:
Tooling -
First-Class Modules
Introduction
Prerequisites : Modules , Functors . First-class modules let you treat modules as values. You can pass them to functions, return them from functions, and store them in data structures. This provid
Module System -
Debugging
The OCaml Debugger
ocamldebug runs on ocamlc bytecode programs (it does not work on native code executables), and it does not work under native Windows ports of OCaml (but it runs under the Cygwin port). We now
Guides