638 search results for ""

Showing 351 - 400
  1. OCaml Programming Guidelines

    Naming Anonymous Functions

    Justification : Much clearer, in particular if the name given to the function is meaningful. <!-- $MDX skip --> write <!-- $MDX skip --> In the case of an iterator whose argument is a comp

    Resources
  2. Functors

    Write a Functor to Extend Modules

    Modules Array and List appear augmented with Array.scan_left and List.scan_left . For brevity, the output of the first two toplevel commands is not shown here. Run the dune utop command. O

    Module System
  3. OCaml on Windows

    Visual Studio Code (VSCode)

    If you use WSL2 , you will remotely connect to your WSL2 instance from VSCode. Microsoft has a useful blog post that covers getting WSL2 and Visual Studio Code connected. Go to File > Prefer

    Resources
  4. Values and Functions

    Local Definitions

    In both examples, d and e are local definitions. Arbitrary combinations of chaining or nesting are allowed. e is bound to 6 inside e * 5 d is bound to 30 inside d * 7 Here is how scopin

    Introduction
  5. Basic Data Types and Pattern Matching

    Introduction

    <!-- The goal of this tutorial is to provide for the following capabilities: - Handle data of all predefined types using dedicated syntax - Write variant type definitions: simple, recursive, and poly

    Introduction
  6. Introduction to opam Switches

    Local Switches

    Local switches are automatically selected based on the current working directory. When you navigate into a directory with an associated local switch, opam uses that switch for any OCaml-related opera

    Tooling
  7. OCaml Programming Guidelines

    Usage in Module Interface

    The function's usage must appear in the module's interface that exports it, not in the program that implements it. Choose comments as in the OCaml system's interface modules, which will subsequen

    Resources
  8. Basic Data Types and Pattern Matching

    User-Defined Types

    These three kinds of type definitions are covered in the next three sections. Variant Record Aliases User-defined types are always introduced using the type … = … statement. The keyword type

    Introduction
  9. Transitioning to Multicore with ThreadSanitizer

    A Proposed Workflow

    Install TSan Write a parallel test runner Run tests under TSan If TSan complains about data races, address the reported issue and go to step 2. Now if we want to see if this code is Multicore read

    Guides
  10. Values and Functions

    Function as Values

    <!-- For now, let's put aside those definitions and instead start playing with functions. Their meaning will arise from experience. Once things make sense, using these terms is just a means to interac

    Introduction
  11. Error Handling

    Asynchronous Exceptions

    The latter is thrown when the user interrupts an interactive execution. Because they are loosely or not at all related with the program logic, it mostly doesn't make sense to track the place wher

    Guides
  12. Debugging

    Detecting a Data Race

    The TSan instrumentation benefits from compiling programs with debug information, which happens by default under dune . To manually invoke the ocamlopt compiler under our 5.2.0+tsan switch

    Guides
  13. Higher Order Functions

    Async code

    <!-- Comment let f () = 42 let f () = g 42 One possible issue with the repeat function used in the intro comes from the fact it is used to produce a side effect. Therefore two stories get mixed tog

    Introduction
  14. Configuring Your Editor

    1) Hovering for Type Information

    This is a great feature that let's you see type information of any OCaml variable or function. All you have to do is place your cursor over the code and it will be displayed in the tooltip. VSCode H

    Tooling
  15. Configuring Your Editor

    Getting Type Information

    OCaml-eglot README provides a comprehensive overview of all the functions available in this mode! Emacs Type information Opening an OCaml file should launch an ocaml-lsp server, and you can co

    Tooling
  16. Installing OCaml

    Install Platform Tools

    You're now all set and ready to start hacking. All these tools can be installed using a single command: UTop , a modern interactive toplevel (REPL: Read-Eval-Print Loop) Dune , a fast and full-fe

    First Steps
  17. Configuring Your Editor

    Getting Type Information

    In the Vim editor, press the Esc to enter command mode. Place the cursor over the variable. Type :MerlinTypeOf and press Enter . The type information will be displayed in the command bar. Othe

    Tooling
  18. Operators

    Using Binary Operators

    Finally, in the third line, all the arguments expected by List.filter are provided. The returned list contains the values satisfying the ( <= ) 10 function. The first shows the List.filter typ

    Advanced Topics
  19. Error Handling

    Safe vs. Unsafe Functions

    The main ways to write such safe error-handling functions are to use either option (next section) or result (following section) values. Although handling errors in data using those types may a

    Guides
  20. Managing Dependencies With opam

    Installing a Git Dependency in Your Switch

    You can install a package in your active switch directly from a Git URL:

    Projects
  21. Functors

    Extending a Module with a Standard Library Functor

    This allows the user to seemingly extend the module String with a submodule Set . Check the behaviour using opam exec -- dune exec funkt < dune . funkt.ml Using the include statement, here i

    Module System
  22. OCaml Programming Guidelines

    Using Tab Stops

    Justification : Between one display and another, the indentation of the program changes completely. It can also become completely wrong if the programmer used both tabulations and spaces to inden

    Resources
  23. Values and Functions

    Anonymous Functions

    Anonymous functions are often passed as arguments to other functions. The identity function, which takes anything and returns it unchanged The square function, which takes an integer and returns it

    Introduction
  24. Values and Functions

    Defining Global Functions

    The former explicitly binds the anonymous function to a name. The latter uses a more compact syntax and avoids the fun keyword and the arrow symbol. The expression, which happens to be a function

    Introduction
  25. Formatting and Wrapping Text

    A Concrete Example

    <!-- $MDX skip --> In Caml Light, replace the first line by: I use the format library to print the lambda-terms: First, I give the abstract syntax of lambda-terms: Thus the problem is to pr

    Tutorials
  26. Basic Data Types and Pattern Matching

    Function Parameter Aliases

    This is useful for matching variant values of parameters. Function parameters can also be given a name with pattern matching for tuples and records.

    Introduction
  27. Configuring Your Editor

    LSP setup for OCaml

    You are now ready to edit OCaml code productively with Emacs! And that's all there is to it! Now all you need to do is install ocaml-lsp-server and ocamlformat in our switch : Next, we n

    Tooling
  28. Modules

    Displaying a Module's Interface

    If you are using Dune, .cmi file are in the _build directory. Otherwise, you can compile manually to generate them. The command ocamlc -c cairo.ml will create cairo.cmo (the executable byteco

    Module System
  29. OCaml Programming Guidelines

    Formatting Guidelines

    Pseudo spaces law : never hesitate to separate words in your programs with spaces. The space bar is the easiest key to find on the keyboard, so press it as often as necessary! If you choose no

    Resources
  30. Introduction to opam Switches

    Global Switches

    Opam's system switch is a global switch that is associated with the OCaml installation on your operating system. The system switch is accessible across the entire system. When creating an opam swi

    Tooling
  31. Profiling

    Polymorphic Types

    Lesson: if you have a function which is unintentionally polymorphic then you can help the compiler by specifying types for one or more of the arguments. Disappointingly although the definition o

    Guides
  32. Objects

    Inheritance and Coercions

    Is it possible to coerce from a superclass (e.g., widget ) to a subclass (e.g., button )? The answer, perhaps surprisingly, is NO! Coercing in this direction is unsafe . You might try to coerc

    Advanced Topics
  33. Hash Tables

    Module Hashtbl

    To find out whether there is an entry in my_hash for a letter we would do: In other contexts, one may prefer new values to replace the previous ones. In this case, one uses Hashtbl.rep

    Data Structures
  34. Installing OCaml

    Initialise opam

    Any problems installing? Be sure to read the latest release notes . You can file an issue at <https://github.com/ocaml/opam/issues> or <https://github.com/ocaml-windows/papercuts/issues> . Note

    First Steps
  35. Monads

    Composition and Monad Laws

    In that formulation, it becomes immediately clear that return is a left and right identity, and that composition is associative. Law 1: return >=> f behaves the same as f . Law 2: f >=> r

    Data Structures
  36. OCaml Programming Guidelines

    Arithmetic operators: the same rules as in mathematics

    For example: 1 + 2 * x means 1 + (2 * x) .

    Resources
  37. OCaml Programming Guidelines

    How to Choose Identifiers

    It's hard to choose identifiers whose name evokes the meaning of the corresponding portion of the program. This is why you must devote particular care to this, emphasising clarity and regularity

    Resources
  38. Fix Homebrew Errors on Apple M1

    Disable Rosetta

    It's essential to close your current Terminal window and open a new one for it to work properly. Then run the following command. If you get the output shown, you're ready to brew! Close Terminal

    Resources
  39. OCaml Programming Guidelines

    Managing Program Development

    Below are tips from veteran OCaml programmers that have served in developing the compilers. These are good examples of large, complex programs developed by small teams.

    Resources
  40. Formatting and Wrapping Text

    Principles

    boxes : a box is a logical pretty-printing unit, which defines a behaviour of the pretty-printing engine to display the material inside the box. break hints : a break hint is a directive to the p

    Tutorials
  41. Values and Functions

    What is a Value?

    An expression's type (before evaluation) and its resulting value's type (after computation) are the same. This allows the compiler to avoid runtime type checks in binaries. In OCaml, the compiler rem

    Introduction
  42. 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
  43. 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
  44. 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
  45. 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
  46. 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
  47. 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
  48. 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
  49. 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
  50. 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