667 search results for ""

Showing 601 - 650
  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. Configuring Your Editor

    Using vim.lsp:

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

    Tooling
  7. OCaml Programming Guidelines

    How to Program

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

    Resources
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. First-Class Modules

    Runtime Selection

    A common pattern is selecting an implementation based on configuration:

    Module System
  16. Sets

    Introduction

    type elt = string for the elements, and type t = Set.Make(String).t for the sets. This module also defines two types: After naming the newly-created module StringSet , OCaml's toplevel displays t

    Data Structures
  17. File Manipulation

    Reading

    Standard in_channel : stdin Commonly used functions: open_in , open_in_bin , close_in , close_in_noerr Open the file to obtain an in_channel Read characters from the channel. Reading consum

    Tutorials
  18. A Tour of OCaml

    Lists

    Note that the x :: v pattern in the second matching expression is used to destructure the list into its head x and tail v , where head is the first element of the list and tail is the rest o

    First Steps
  19. Higher Order Functions

    Mapping

    This is called mapping . For example, if we have a list of users, maybe we want to get a list of usernames. Or if we have an optional password, we may want to encrypt it only if it is set. In cont

    Introduction
  20. File Manipulation

    Writing

    Standard out_channel s: stdout , stderr Commonly used functions: open_out , open_out_bin , flush , close_out , close_out_noerr Open the file to obtain an out_channel Write to the channel

    Tutorials
  21. Profiling

    Arrays

    So arrays of floats are unboxed, as expected. (Each float in the array is 8 bytes long.) The assembler syntax is rather complex, but the bracketed expression -4(%ecx, %eax, 4) means "at the ad

    Guides
  22. A Tour of OCaml

    Conclusion

    In this tutorial, OCaml was used interactively. The next tutorial, Your First OCaml Program , shows you how to write OCaml files, how to compile them, and how to kickstart a project. <!-- 1. Values

    First Steps
  23. Sets

    Creating a Set

    There's another relevant function StringSet.of_seq: string Seq.t -> StringSet.t that creates a set from a sequence . Converting a list into a set using StringSet.of_list : A set with a sing

    Data Structures
  24. Preprocessors and PPXs

    Writing a PPX

    If you want to write your own PPX, the place to start is ppxlib's documentation .

    Advanced Topics
  25. Lists

    Sorting Lists

    The function Fun.flip reverses a binary function parameter order. The function List.sort , given a comparison function of type 'a -> 'a -> int (zero if equal, negative if first smaller, p

    Introduction
  26. Options

    Conclusion

    The option type in OCaml provides a powerful and type-safe way to represent values that may be absent, avoiding the pitfalls of exceptions. By leveraging functions such as map , join , get , va

    Data Structures
  27. Error Handling

    Runtime Crashes

    Although OCaml is a very safe language, it is possible to trigger unrecoverable errors at runtime.

    Guides
  28. Maps

    Checking if a Key is Contained in a Map

    To check if a key is a member of a map, use the mem function:

    Data Structures
  29. Profiling

    Summary

    Write your program as simply as possible. If it takes too long to run, profile it to find out where it's spending its time and concentrate optimisations on just those areas. Check for unintention

    Guides
  30. File Manipulation

    Seeking

    Whenever you write or read something to or from a channel, the current position changes to the next character after what you just wrote or read. Occasionally, you may want to skip to a particular

    Tutorials
  31. Sets

    Subtracting a Set from Another

    With the function StringSet.diff , we can remove the elements of the second set from the first set.

    Data Structures
  32. Modules

    Introduction

    Note : The files that illustrate this tutorial are available as a Git repo . Modules are collections of definitions grouped together. This is the basic means to organise OCaml software. Separate co

    Module System
  33. Profiling

    Floats

    string_of_float isn't polymorphic, but suppose we have a polymorphic function foo : 'a -> unit taking one polymorphic argument. If we call foo with %eax containing 7, then this is equivalen

    Guides
  34. Comparison of Standard Containers

    Stack

    Adding an element: O(1) Taking an element: O(1) Length: O(1) OCaml stacks are mutable last-in-first-out (LIFO) data structures. They are just like lists except they are mutable, i.e., adding an

    Resources
  35. Libraries With Dune

    Conclusion

    The OCaml module system allows organising a project in many ways. Dune provides several means to arrange modules into libraries.

    Module System
  36. Objects

    Objects Without Class

    Here we examine how to use objects pretty much like records, without necessarily using classes.

    Advanced Topics
  37. Basic Data Types and Pattern Matching

    Variants

    Variants are also called tagged unions . They relate to the concept of disjoint union .

    Introduction
  38. Lists

    Folds

    Here are some more redefinitions of familiar functions in terms of fold_left or fold_right . Can you work out how they operate? Unfortunately, the order of evaluation here is such that larg

    Introduction
  39. Functors

    Conclusion

    Functor application essentially works the same way as function application: passing parameters and getting results. The difference is that we are passing modules instead of values. Beyond comfort, it

    Module System
  40. Maps

    Creating a Map

    Example 2 : By Adding an Element Example 1 : By Annotation When you create your new map with an annotation By adding an element to the map: The type of the values can be specified in two ways: T

    Data Structures
  41. Configuring Your Editor

    Emacs

    A major mode, which, among other things, supports syntax highlighting and the structuring of indentation levels A minor mode, which will interact with a language server (such as ocaml-lsp-server or

    Tooling
  42. Error Handling

    Documentation

    <!-- $MDX skip --> Functions that can raise exceptions should be documented like this:

    Guides
  43. Maps

    Merging Maps

    pick_fst picks the result's value from the first map pick_snd picks the result's value from the second map drop drops both entries in the result map Here are examples of duplicate key resolut

    Data Structures
  44. Loops and Recursions

    Approach 2

    The imperative approach uses a while loop that is broken out of using an exception.

    Introduction
  45. Profiling

    Speed

    But you will need to know some assembler to get the most out of this section. Don't be afraid! I'll help you out by translating the assembler into a C-like pseudocode (after all C is just a porta

    Guides
  46. Sets

    Intersection of Two Sets

    With the function StringSet.inter , we can compute the intersection of two sets.

    Data Structures
  47. Arrays

    Map an Array

    The Array.map function creates a new array by applying a given function to each element of an array. For example, we can get an array containing the square of each number in the even_numbers ar

    Data Structures
  48. Arrays

    Conclusion

    In this tutorial, we covered the basics of arrays in OCaml, including how to create and manipulate them, as well as some of the most useful functions and use cases. Please refer to the standard libr

    Data Structures
  49. OCaml on Windows

    WSL2

    After you have installed WSL2 and chosen one Linux distribution (we suggest Ubuntu LTS ), you can follow the Installing OCaml: Installation for Linux and macOS steps. If you only need to run O

    Resources
  50. Comparison of Standard Containers

    Queue

    Adding an element: O(1) Taking an element: O(1) Length: O(1) OCaml queues are mutable first-in-first-out (FIFO) data structures.

    Resources