715 search results for ""

Showing 451 - 500
  1. Introduction to opam Switches

    Creating a Global Switch

    This switch is now available from any directory. It lives at ~/.opam/my_switch/ . Then update your shell environment: To create a named global switch:

    Tooling
  2. 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
  3. 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 manually

    Introduction
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. OCaml Docker Images

    Pinning Images

    Trade-off : Pinned images don't get automatic updates, so you must manually update the digest to receive security patches. Finding a digest: Reproducibility : When you pull an image by its digest,

    Additional Tooling
  13. Configuring Your Editor

    MELPA and use-package

    The use-package macro has been built into Emacs since version 29.1. If you are on an older version, you will need to install use-package manually. You will also need to ensure MELPA is configur

    Tooling
  14. Modules

    Module Signatures are Types

    This allows writing interfaces shared by several modules. An implementation satisfies any module type listing some of its contents. This implies a module may have several types and that there is a su

    Module System
  15. Libraries With Dune

    Introduction

    Note : The other terms we use are classifications of clouds. These include " cumulus " (fluffy), " nimbus " (precipitating), " cumulonimbus " (fluffy and precipitating), " nimbostratus " (flat, amorp

    Module System
  16. Sequences

    Sequence Producers: Functions as Results

    A producer is a function that generates a sequence. Producers return a function so that elements are only computed when needed. This ensures deferred evaluation and avoids unnecessary computatio

    Data Structures
  17. Functors

    Naming and Scoping

    Warning : Don't shadow names too often because it makes the code harder to understand. In the example above, t from with type takes precedence over the local t , which only has a local scope.

    Module System
  18. Modules

    Naming and Scoping

    The module access notation can be applied to an entire expression: You can open a module inside a definition, using the let open ... in construct: The standard library is a module called Std

    Module System
  19. Values and Functions

    Conclusion

    <!-- One may wonder: > Why is function-as-values the key concept of functional programming? The answer to this question goes beyond the scope of this tutorial. This comes from the [λ-calculus](htt

    Introduction
  20. Loops and Recursions

    Looping Over Strings

    String.copy copies a string, like strdup . There is also a String.iter function which works like List.iter , except over the characters of the string. The String module also contains dozen

    Introduction
  21. OCaml Programming Guidelines

    Subdivide Your Programs Into Little Functions

    Small functions are easier to master.

    Resources
  22. Hash Tables

    Removing Data from Hash Tables

    This behavior is interesting for the above example or when, say, the keys represent variables that can be temporarily masked by a local variable of the same name. If you remove a key, its prev

    Data Structures
  23. Monads

    Example: The Lwt Monad

    Metaphorically, as we discussed before, the box involved here is one that starts out empty but eventually will be filled with a value of type 'a . The "something more" in these computations is t

    Data Structures
  24. Basic Data Types and Pattern Matching

    Records

    Note that records behave like single constructor variants. That allows pattern matching on them. To construct a new record with some field values changed without typing in the unchanged fields w

    Introduction
  25. OCaml Programming Guidelines

    Consistency of Indentation

    Choose a generally accepted style of indentation, then use it systematically throughout the whole application.

    Resources
  26. OCaml Programming Guidelines

    match or try

    Justification : The keyword with on its own line shows that the program enters the pattern-matching part of the construct. <!-- $MDX skip --> Put the keyword with at the end of the line. I

    Resources
  27. OCaml Docker Images

    Supported Distributions

    Debian (various versions) Ubuntu (including ubuntu-lts for latest LTS) Alpine Fedora openSUSE Arch Linux (rolling release, just archlinux tag) Windows : windows-mingw-* (MinGW-w64) and wind

    Additional Tooling
  28. Using OCaml with GitHub Actions

    Key Features

    | Feature | Input | Description | |---------|-------|-------------| | Compiler version | ocaml-compiler: 5.2 | Supports semver syntax | | Compiler variants | ocaml-compiler: ocaml-variants.5

    Additional Tooling
  29. Functors

    Replacing a Dependency

    Note : Modules received and returned by IterPrint.Make both have a type t . The with type ... := ... constraint exposes that the two types t are the same. This makes functions from the injecte

    Module System
  30. Lists

    The Standard Library List Module

    In the List module documentation, functions which can raise an exception are marked. Such exceptions are usually the result of lists which are empty (and therefore have neither a head nor a tai

    Introduction
  31. Higher Order Functions

    Mapping Results

    Both of these are useful in different situations, such as wanting to change the type of errors, or only perform operations once we have an Ok value. We can map the value in the Ok value constr

    Introduction
  32. Libraries With Dune

    Libraries

    Dune creates a module Wmo from the contents of directory lib . The directory's name (here lib ) is irrelevant. The library name appears uncapitalised ( wmo ) in dune files: In its definition, i

    Module System
  33. OCaml Programming Guidelines

    Write Simple and Clear Programs

    Reread, simplify, and clarify at every stage of creation. Use your head!

    Resources
  34. Operators

    Defining Binary Operators

    It is a recommended practice to define operators in two steps, like shown in the example. The first definition contains the function's logic. The second definition is merely an alias of the first one

    Advanced Topics
  35. Higher Order Functions

    Uncurrying

    But we can also uncurry our greet function to operate over the entire tuple! If we wanted to do something with any of these elements, we'd need to split the tuple, and call a function: Take

    Introduction
  36. Values and Functions

    Same-Level Shadowing

    There are now two definitions of h in the environment. The first h is unchanged. When the second h is defined, the first one becomes unreachable. Another kind of shadowing takes place when t

    Introduction
  37. A Tour of OCaml

    Variant Types

    As previously shown, sum , length , and map functions provide examples of pattern matching over the list variant type. Like a function, a variant can be recursive if it refers to itself in its

    First Steps
  38. Using OCaml with GitHub Actions

    Cross-Platform Support

    Works on Linux, macOS, and Windows with the same workflow file. The action handles platform-specific opam setup automatically.

    Additional Tooling
  39. Transitioning to Multicore with ThreadSanitizer

    Following the Workflow

    We will now go through the proposed workflow for our example application.

    Guides
  40. Installing OCaml

    Check Installation

    Exit UTop by typing #quit;; (the # here is not the prompt, you have to type it) or pressing Ctrl+D . Congratulations ! You've installed OCaml! 🎉 You're now in an OCaml toplevel, and you c

    First Steps
  41. Error Handling

    Assertions

    Here, it wouldn't be correct to use failwith because it requires a corrupted system or the compiler to be bugged for the second code path to be executed. Breakage of the language semantics qual

    Guides
  42. Basic Data Types and Pattern Matching

    Tuples

    <!--FIXME :: Please ensure this is still correct. In trying to keep examples away from a, b, c (so as not to be confused with `a, `b, `c), I thought it was particularly important here because the use

    Introduction
  43. First-Class Modules

    Using Type Constraints

    The (type a) syntax creates a locally abstract type that connects the module's type t with the list elements' type. When you need to expose the type from a first-class module, use type constra

    Module System
  44. Configuring Your Editor

    Using nvim-lspconfig

    There is no need to pass more settings to setup because nvim-lspconfig provides reasonable defaults. See the nvim-lspconfig OCaml LSP configuration for more info. Add this to your nvim-lspco

    Tooling
  45. OCaml Programming Guidelines

    for loops

    Justification : The recursive function lets you code any loop whatsoever simply, even a complex one, e.g., with multiple exit points or with strange index steps (steps depending on a data value

    Resources
  46. Debugging

    Using the Debugger Under Emacs

    Under Emacs you call the debugger using ESC-x ocamldebug a.out . Then Emacs will send you directly to the file and character reported by the debugger, and you can step back and forth using ES

    Guides
  47. Profiling

    Using perf on Linux

    The first command launches foo.native with arguments a b c d and records profiling information in perf.data ; the second command starts an interactive program to explore the call graph. The

    Guides
  48. Higher Order Functions

    Mapping Options

    Note that both sides of the match return the same thing: if we had a None we return None , if we have a Some we return a Some . This way, the structure is preserved. Mapping an optional valu

    Introduction
  49. Configuring Your Editor

    Finer configuration

    OCaml-eglot can be finely configured, the project README gives several configuration paths to adapt perfectly to your workflow. You will also find there an exhaustive presentation of the different

    Tooling
  50. Mutability and Imperative Control Flow

    For Loop

    Note: Here is how to do the same thing using an iterator function: for loops are convenient to iterate over and modify arrays: When you use the downto keyword (instead of the to keyword),

    Introduction