667 search results for ""

Showing 401 - 450
  1. OCaml Programming Guidelines

    Sequences inside branches of if

    <!-- $MDX skip --> In the same way, a sequence which appears in the then or else part of a conditional must be delimited:

    Resources
  2. OCaml Programming Guidelines

    Height of the Page

    Justification : When a function goes beyond one screenful, it's time to divide it into subproblems and handle them independently. Beyond a screenful, one gets lost in the code. The indentation is

    Resources
  3. Debugging

    Installing a TSan Switch

    If the above fails during installation of conf-unwind with No package 'libunwind' found , try setting the environment variable PKG_CONFIG_PATH to point to the location of libunwind.pc , for

    Guides
  4. A Tour of OCaml

    Defining a Higher-Order Function

    It is possible to pass a function as argument to another function. Functions having other functions as parameters are called higher-order functions. This was illustrated earlier using function L

    First Steps
  5. Monads

    The Monad Signature

    So the next few sections look at several different examples of code in which monads can be discovered. Because monads are a design pattern, they aren't always obvious; it can take some study to t

    Data Structures
  6. OCaml Programming Guidelines

    Subdividing into modules

    For each interface, you must document the things defined by the module: functions, types, exceptions, etc. For each module, you must explicitly write an interface. You must subdivide your program

    Resources
  7. OCaml on Windows

    Diskuv OCaml

    To learn more about Diskuv OCaml, see the official Diskuv OCaml documentation . Check that OCaml is installed properly with the following commands in your shell (PowerShell or Command Prompt).

    Resources
  8. Running Commands in an opam Switch

    Using opam exec

    This will launch the version of the OCaml REPL within the context of the current opam switch. Example: Replace <command> with the actual command you want to run. This ensures that the command is

    Projects
  9. Options

    Peel Off Doubly Wrapped Options

    Here, safe_divide may return Some or None . Option.join ensures we don’t get Some None . Example 2 : Nested Option with a Computation If Some (Some v) , join extracts v and retur

    Data Structures
  10. Profiling

    Using Instruments on macOS

    From there, you can click on your program there and dig to see which functions are taking the longest to execute. <img width="100%" alt="macOS Instruments" src="/media/tutorials/macos-instruments.

    Guides
  11. Running Commands in an opam Switch

    Using opam env

    This command evaluates the output of opam env and sets the necessary environment variables for the currently active switch. After running this command, you'll have access to the packages installed in

    Projects
  12. Configuring Your Editor

    Visual Studio Code

    Upon first loading an OCaml source file, you may be prompted to select the toolchain in use. Pick the version of OCaml you are using, e.g., 5.1.0 from the list. For VSCode, install the OCaml Pl

    Tooling
  13. Sequences

    Iterating Over Sequences

    The key point is that it doesn't leak memory. This example runs in constant space. It is effectively nothing more than an infinite loop, which can be confirmed by monitoring the space consumption

    Data Structures
  14. Error Handling

    Exceptions Not Raised

    This has improved since. Only linked C code should be able to trigger an undetected stack overflow. Xavier Leroy, October 2021 But catching stack overflows is tricky, both in Unix-like systems

    Guides
  15. Objects

    The Oo Module and Comparing Objects

    = and <> can be used to compare objects for physical equality (an object and its copy are not physically identical). You can also use < etc., which provides an ordering of objects based app

    Advanced Topics
  16. Error Handling

    Conversion Between option and result

    From option to result , use function Option.to_result : From result to option , use function Result.to_option : This is done by using the following functions:

    Guides
  17. Values and Functions

    The Application Operator

    The @@ application operator applies an argument (on the right) to a function (on the left). It is useful when chaining several calls, as it avoids writing parentheses, which creates easier-to-read

    Introduction
  18. Sequences

    Unfolding Sequences

    The function Seq.uncons returns the head and tail of a sequence if it is not empty. Otherwise, it returns None . We can check our map function by applying a square function to a sequence:

    Data Structures
  19. Sequences

    Introduction

    In OCaml, any value a of type t can be turned into a constant function by writing fun _ -> a or fun () -> a . The latter function is called a thunk . Using this terminology, Seq.t value

    Data Structures
  20. Debugging

    Polymorphic Functions

    For our sorting routine, a single type constraint on the argument of the exchange function warranties a monomorphic typing, that allows a proper trace of function calls: A simple way to overc

    Guides
  21. Values and Functions

    Inner Shadowing

    <!-- A name-value pair in a local expression *shadows* a binding with the same name in the global environment. In other words, the local binding temporarily hides the global one, making it inaccessib

    Introduction
  22. Fix Homebrew Errors on Apple M1

    Return to Install Tutorial

    You're all set to keep learning OCaml! Now that's all sorted, you can return to the Install OCaml tutorial to install and initialise opam.

    Resources
  23. Preprocessors and PPXs

    PPX Rewriters

    Secondly, most of the PPX's code transformation do not need to be given the full AST; they can work locally in subparts of it. There are two kinds of such local restrictions to the general PPX re

    Advanced Topics
  24. OCaml Programming Guidelines

    When to Use Mutables

    Mutable values are useful and sometimes indispensable for simple and clear programming. Nevertheless, you must use them with discernment because OCaml's normal data structures are immutable. They

    Resources
  25. Your First OCaml Program

    Minimum Setup

    Note : minimo.exe is not a file name. This is how Dune is told to compile the minimo.ml file using OCaml's native compiler instead of the bytecode compiler. As a fun fact, note that an empty file

    First Steps
  26. Error Handling

    Concluding Remarks

    Properly handling errors is a complex matter. It is cross-cutting concern , touches all parts of an application, and can't be isolated in a dedicated module. In contrast to several other mains

    Guides
  27. Debugging

    Detecting a Data Race with Thread Sanitizer

    With the introduction of Multicore parallelism in OCaml 5, comes the risk of introducing data races among the involved Domain s. Luckily the Thread Sanitizer (TSan) mode for OCaml is helpful to

    Guides
  28. Modules

    File-Based Modules

    In a project, it is preferable to create the dune configuration files and directory structure using the dune init project command. Refer to the Dune documentation for more on this matter. Ac

    Module System
  29. Values and Functions

    Defining Local Functions

    Although local functions are often defined inside the function's scope, this is not a requirement. The function sq is only available inside the sq 7 * sq 7 expression. Calling sq gets an erro

    Introduction
  30. How to Work with the Garbage Collector

    Exercises

    Implement the record as an object , and allow it to transparently pad/unpad strings. You will need to provide methods to set and get the name and address fields (four public methods in all). Hid

    Guides
  31. OCaml Programming Guidelines

    Use Assertions

    Note as well that an assertion is often preferable to a comment because it's more trustworthy. An assertion is forced to be pertinent because it is verified upon each execution, while a comment c

    Resources
  32. First-Class Modules

    Passing Modules to Functions

    The pattern (module P : Printer) automatically unpacks the first-class module, making P available as a regular module inside the function. You can write functions that accept modules as parame

    Module System
  33. Error Handling

    Naming Conventions

    However, some projects tend to avoid or reduce the usage of exceptions. In such a context, the opposite convention is relatively common: the version of the function that raises exceptions is su

    Guides
  34. Mutability and Imperative Control Flow

    Imperative Control Flow

    OCaml allows you to evaluate expressions in sequence and provides for and while loops to execute a block of code repeatedly.

    Introduction
  35. Using the OCaml Compiler Toolchain

    Other Build Systems

    OMake Another OCaml build system. GNU make GNU make can build anything, including OCaml. May be used in conjunction with OCamlmakefile Oasis Generates a configure, build, and install system from

    Guides
  36. Objects

    Polymorphic Classes

    We can force OCaml to be more specific and only allow drain_stack to be called on 'a stack s by narrowing the type of the s argument, like this: Notice the type of drain_stack . Cleverly

    Advanced Topics
  37. Using the OCaml Compiler Toolchain

    Compilation Basics

    In this section, we will first see how to compile a simple program using only ocamlc or ocamlopt . Then we will see how to use libraries and how to take advantage of the findlib system, wh

    Guides
  38. OCaml Programming Guidelines

    Pattern-Matching

    Never be afraid of overusing pattern-matching! On the other hand, be careful to avoid nonexhaustive pattern-matching constructs. Complete them with care, without using a “catch-all” clause su

    Resources
  39. Objects

    Class Types vs. Just Types

    Mixing objects that share a common subtype can be done, but it requires explicit type coercion using the :> operator: In this example, t is also the type of objects that this class would

    Advanced Topics
  40. Basic Data Types and Pattern Matching

    Integers

    There are no dedicated types for unsigned integers in OCaml. Bitwise operations on int treat the sign bit the same as other bits. Binary operators use standard symbols. The signed remainder operato

    Introduction
  41. Higher Order Functions

    Readability Notes

    Versus a much more readable and easier to maintain version: For example, this is a pipeline with a lot of currying/uncurrying that would most likely be easier to read and maintain if we manuall

    Introduction
  42. Sequences

    Constructing Sequences

    The OCaml Standard Library contains a module for sequences called Seq . It contains Seq.int , which we implemented above. The function ints n looks as if building the infinite sequence (n; n +

    Data Structures
  43. Values and Functions

    Global Definitions

    Global definitions are those entered at the top level. Here, the_answer is defined globally. This is what happens when writing a definition in UTop: If the expression can be evaluated, it is. Ot

    Introduction
  44. OCaml Programming Guidelines

    while loops

    Justification : while loops require one or more mutables, so the loop condition changes value and the loop finally terminates. To prove their correctness, you must discover the loop invarian

    Resources
  45. Values and Functions

    Pattern Matching on Tuples

    The List.split function turns a list of pairs into a pair of lists. Here, each resulting list is bound to a name. A common case is tuples. It allows the creation of two names with a single let .

    Introduction
  46. OCaml Programming Guidelines

    Exceptions

    Justification : try ... with _ -> silently catches all exceptions, even those which have nothing to do with the computation at hand (for example, an interruption will be captured and the

    Resources
  47. Sets

    Sets With Custom Comparators

    You can use any type for elements, as long as you define a meaningful compare operation. The value "HELLO" is not added to the set because it is considered equal to the value "hello" , which

    Data Structures
  48. Monads

    Monad Laws

    But the problem is that doesn't type check: f >>= g doesn't have the right type to be on the right-hand side of >>= . So we have to insert an extra anonymous function fun x -> ... to make th

    Data Structures
  49. Configuring Your Editor

    Editor Features at Your Disposal

    If your editor is setup correctly, here are some important features you can begin using to your advantage:

    Tooling
  50. OCaml Programming Guidelines

    Compiler Warnings

    Compiler warnings are meant to prevent potential errors, which is why you absolutely must heed them and correct your programs if compiling them produces such warnings. Besides, programs whose com

    Resources