715 search results for ""

Showing 601 - 650
  1. Higher Order Functions

    Iterating

    Iterating in OCaml means that if there is one value (or more), we'd like to apply a function to it. But in OCaml the pattern for iteration can be extended to other kinds of data types, like optional

    Introduction
  2. OCaml Docker Images

    Dev Containers

    For a development-oriented Docker setup with OCaml LSP, editors, and debugging tools pre-installed, see the OCaml devcontainer . It works with VS Code, GitHub Codespaces, and the DevContainer CLI.

    Additional Tooling
  3. The Compiler Backend: Bytecode and Native code

    Gprof

    Getting precise information out of gprof requires passing the -p flag to the native code compiler when compiling and linking the binary. This generates extra code that records profile infor

    Runtime & Compiler
  4. Functors

    Project Setup

    Check that this works using the opam exec -- dune exec funkt command. It shouldn't do anything (the empty file is valid OCaml syntax), but it shouldn't fail either. The stanza libraries str makes

    Module System
  5. Lists

    Functions on Lists

    Notice that the memory for the second list is shared, but the first list is effectively copied. Why is this? Because in the pattern _ :: t the head of the list is not inspected, so its type

    Introduction
  6. Lists

    Maps and Iterators

    Notice that map2 and iter2 will fail if the lists are of unequal length: There is a variant iter2 for two lists too: In addition, we have an imperative analogue to map , called

    Introduction
  7. A Tour of OCaml

    Records

    Here, the pattern { age = x; _ } is typed with the most recently declared record type that has an age field of type int . The type int is inferred from the expression 13 <= x && x <= 19 . The

    First Steps
  8. OCaml on Windows

    Docker Images

    The ocaml/opam Docker Hub repository now contains regularly-updated Windows images. This includes images using msvc and mingw . If you are comfortable with Docker, this might be an easier w

    Resources
  9. Maps

    Introduction

    When we created the StringMap module, we fed the Map.Make functor the String module to define the type of the map's keys, which we can observe in the StringMap 's signature ( type key stri

    Data Structures
  10. Modules

    Module Inclusion

    It creates a module Extlib.List that has everything the standard List module has, plus a new uncons function. In order to override the default List module from another .ml file, we need

    Module System
  11. Basic Data Types and Pattern Matching

    Unit

    The function print_endline prints the string followed by a line ending on standard output. Return of the unit value means the output request has been queued by the operating system. Note : Replace

    Introduction
  12. Using the OCaml Playground

    Autocomplete

    Autocomplete in the OCaml Playground The playground also supports code completion. It helps users by suggesting and completing their input based on the context.

    Resources
  13. First-Class Modules

    Key Points

    Pack modules with (module M : ModuleType) Unpack with let module M = (val x : ModuleType) in ... Functions can directly pattern match: fun (module M : ModuleType) -> ... Use with type constraint

    Module System
  14. Using the OCaml Playground

    Bottom Line

    Congratulations! You have made it to the end. Hopefully, by now, you have a better idea how to use the OCaml Playground . Use this to practice the OCaml code and have fun. Happy Hacking!

    Resources
  15. First-Class Modules

    Conclusion

    For compile-time module generation and maximum performance, use functors instead. First-class modules let you write flexible code that chooses implementations at runtime. They're particularly useful

    Module System
  16. Sets

    Adding an Element to a Set

    The function StringSet.add with type string -> StringSet.t -> StringSet.t takes both a string and a string set. It returns a new string set. Sets created with the Set.Make functor in OCaml are

    Data Structures
  17. Modules

    Stateful Modules

    Values returned by Random.bits will differ when you run this code. The first and third calls return the same results, showing that the internal state was reset. A module may have an internal

    Module System
  18. OCaml-CI

    Step 3: Get Approved

    You will need to be approved before it will start building anything. This is done by adding yourself to a list by submitting a PR to the ocaml-ci repository, specifically adding yourself to deploy

    Additional Tooling
  19. Lists

    List Searching

    Note that the documentation for filter and partition tells us that the order of the input is preserved in the output. Where this is not stated it the documentation, it cannot be assumed.

    Introduction
  20. Arrays

    The Standard Library Array Module

    OCaml provides several useful functions for working with arrays. Here are some of the most common ones:

    Data Structures
  21. Installing OCaml

    For macOS

    Note : While it's rather straightforward to install opam using macOS, it's possible you'll run into problems later with Homebrew because it has changed the way it installs. The executable files canno

    First Steps
  22. Arrays

    Iterate on an Array

    Iterating on arrays can also be made using for loops. Here is the same example using a loop: Array.iter applies a function to each element of an array, one at a time. The given function must

    Data Structures
  23. Configuring Your Editor

    Neovim

    A newer, more recommended way is to use the new Neovim LSP API for versions newer than v0.11.0 via vim.lsp . A more traditional way is to use nvim-lspconfig . For more info, kickstart.nvim has a

    Tooling
  24. Using the OCaml Playground

    Caveat

    In contrast, when you separate these expressions with a ;; , like this , or when you bind them to names, like this , they are evaluated successfully, one after another. A little caveat here is

    Resources
  25. Error Handling

    Stack Traces

    And you will get a stack trace. Alternatively, you can call, from within the program, To get a stack trace when an unhandled exception makes your program crash, you need to compile the progr

    Guides
  26. Maps

    Removing Entries From a Map

    Note that the initial map lucky_numbers remains unchanged. Removing a key that isn't present in the map has no effect. To remove an entry from a map, use the remove function, which takes a ke

    Data Structures
  27. Arrays

    Introduction

    Storing and processing large amounts of data Implementing algorithms that require random access and modification of elements Working with matrices and other multi-dimensional data structures Arrays

    Data Structures
  28. Higher Order Functions

    Let-ops

    This has the advantage of making code a lot more readable, without changing the behavior we've come to expect from bind calls. Thankfully, OCaml lets us redefine a subset of operators called let-

    Introduction
  29. Options

    Introduction

    The option type is useful when the lack of data is better handled as the special value None rather than an exception. It is the type-safe version of returning error values. Since data wrapped in an

    Data Structures
  30. Higher Order Functions

    Binding

    To do this with lists we can use the concat_map function, which looks like this: For example, if we have a list and we map over it with a function that returns a list, then we'll have a list of

    Introduction
  31. Higher Order Functions

    Sorting

    Most OCaml modules include a compare function that can be passed in to sort : For lists, this operation returns a new sorted list: For arrays, this operation mutates the array in-place: B

    Introduction
  32. Functors

    Introduction

    Note : The files illustrating this tutorial are available as a Git repo . As suggested by the name, a functor is almost like a function. However, while the inputs and outputs of functions are val

    Module System
  33. Arrays

    Sorting an Array

    It sorts the provided array in place and in ascending order, according to the provided comparison function. Sorting performed by Array.sort modifies the content of the provided array, which is wh

    Data Structures
  34. A Tour of OCaml

    Exceptions

    The standard library provides several predefined exceptions. It is possible to define exceptions. Exceptions are caught using the try … with … construction: Note that exceptions do not appe

    First Steps
  35. Loops and Recursions

    Approach 1

    Get the length of the file and read it all at once using the really_input method. This is the simplest, but it might not work on channels that are not really files (e.g., reading keyboard input)

    Introduction
  36. Maps

    Adding Entries to a Map

    Note that the initial map lucky_numbers remains unchanged. If the passed key is already associated with a value, the passed value replaces it. To add an entry to a map, use the add function t

    Data Structures
  37. OCaml Programming Guidelines

    How to Program

    Always put your handiwork back on the bench, then polish it and repolish it.

    Resources
  38. OCaml Programming Guidelines

    Credits

    Thanks to all those who have already participated in the critique of this page: Daniel de Rauglaudre, Luc Maranget, Jacques Garrigue, Damien Doligez, Xavier Leroy, Bruno Verlyck, Bruno Petazzoni,

    Resources
  39. Sets

    Checking if an Element is Contained in a Set

    To check if an element is contained in a set, use the StringSet.mem function.

    Data Structures
  40. Error Handling

    Printing

    Each printer should take care of the exceptions it knows about, returning Some <printed exception> , and return None otherwise (let the other printers do the job). OCaml knows how to print it

    Guides
  41. OCaml Docker Images

    Special Tags

    archive - Contains a snapshot of all source archives for opam-repository (useful for setting up a local cache) SHA256 hashes - Used by OCaml-CI for reproducible builds

    Additional Tooling
  42. Sets

    Removing an Element from a Set

    The function StringSet.remove with type string -> StringSet.t -> StringSet.t takes both a string and a string set. It returns a new string set without the given string.

    Data Structures
  43. Lists

    List Scanning

    So you can see how the standard library has evolved into its present state: pieces of frequently-used code are turned into useful general functions. This is rather clumsy, though. The standard l

    Introduction
  44. Modules

    Conclusion

    Functors, which act like functions from modules to modules Libraries, which are compiled modules bundled together Packages, which are installation and distribution units Going further, here are the o

    Module System
  45. Labelled and Optional Arguments

    Conclusion

    Functions can have named or optional parameters. Refer to the reference manual for more examples and details on labels.

    Introduction
  46. OCaml-CI

    Shorthand Syntax

    Example: You can omit refs/ from references (e.g., heads/master instead of refs/heads/master ) For PRs, you can omit /head (e.g., pull/867 instead of refs/pull/867/head ) For commits, pro

    Additional Tooling
  47. Configuring Your Editor

    Using vim.lsp

    See :h lsp-config for more detail on configuration options. Add this to your toplevel init.lua .

    Tooling
  48. OCaml-CI

    Example Commands

    cancel - Cancel a running build rebuild - Trigger a rebuild status - Check build status Other actions: View the build log (follows if still running): List build variants for a specific ref:

    Additional Tooling
  49. OCaml Docker Images

    Tag Format

    The latest tag is the latest release of OCaml running on the latest version of Debian (Debian is used as it has the widest architecture support for OCaml). Images are tagged with distribution an

    Additional Tooling
  50. First-Class Modules

    Runtime Selection

    A common pattern is selecting an implementation based on configuration:

    Module System