667 search results for ""

Showing 551 - 600
  1. Sequences

    Consumers vs Producers

    A function with a sequence parameter consumes it; it's a sequence consumer. A function with a sequence result produces it; it's a sequence producer. In both cases, consumption and production occurs o

    Data Structures
  2. First-Class Modules

    Basic Example

    The expression (module SimplePrinter : Printer) converts a module into a value of type (module Printer) . The expression (val printer : Printer) converts it back. The type annotation can often b

    Module System
  3. Arrays

    Modifying Array Elements

    Note that this operation returns unit , not the modified array. even_numbers is modified in place as a side effect. To modify an element in an array, we simply assign a new value to it using th

    Data Structures
  4. Debugging

    Setting Break Points

    Now we can guess why List.assoc will fail to find "INRIA" in the list... Then, we can step and find what happens just before ( <|b|> ) List.assoc is about to be called in find_address : L

    Guides
  5. Configuring Your Editor

    Windows Users

    If you used the DkML distribution, you will need to: 1. Go to File > Preferences > Settings view (or press Ctrl , ) 2. Select User > Extensions > OCaml Platform 3. Uncheck OCaml: U

    Tooling
  6. Your First OCaml Program

    Conclusion

    <!-- TODO: link Project Quickstart If you're already familiar with lists, maps, and folds, and need to be productive as fast as possible, dive into the “Project Quickstart” guide. --> This tuto

    First Steps
  7. Modules

    Submodule With Signatures

    The first version made Florence.Hello.message public. In this version it can't be accessed from glasgow.ml . To define a submodule's interface, we can provide a module signature . This is do

    Module System
  8. Arrays

    Accessing Array Elements

    You can access individual elements of an array using the .(index) syntax, with the index of the element you want to access. The index of the first element is 0, and the index of the last element

    Data Structures
  9. Basic Data Types and Pattern Matching

    Options

    Operations on options are provided by the Option module. Options are discussed in the Error Handling guide. Here is an example of pattern matching on an option value: The option type is a

    Introduction
  10. Values and Functions

    The Pipe Operator

    This is just like a Unix shell pipe. The pipe operator ( |> ) also avoids parentheses but in reversed order: function on right, argument on left.

    Introduction
  11. OCaml Programming Guidelines

    Delimiters

    A space should always follow a delimiter symbol, and spaces should surround operator symbols. It has been a great step forward in typography to separate words by spaces in order to make written t

    Resources
  12. A Tour of OCaml

    Anonymous Functions

    We can write anonymous functions and immediately apply them to a value: Anonymous functions do not have a name, and they are defined with the fun keyword:

    First Steps
  13. Basic Data Types and Pattern Matching

    Type Aliases

    This is mostly useful as a means of documentation or to shorten long type expressions. Just like values, any type can be given a name.

    Introduction
  14. Installing OCaml

    Join the Community

    Make sure you join the OCaml community . You'll find many community members on Discuss or Discord . These are great places to ask for help if you have any issues.

    First Steps
  15. Operators

    Unary Operators

    This allows users to write more compact code. However, be careful not to write excessively terse code, as it is harder to maintain. Understanding operators must be obvious to most readers, otherwise

    Advanced Topics
  16. Lists

    Association Lists

    When using association lists, and for other purposes, it is sometimes useful to be able to make a list of pairs from a pair of lists and vice versa. The List module provides the functions sp

    Introduction
  17. The Compiler Backend: Bytecode and Native code

    Perf

    <div class="note"> Perf has a growing collection of other commands that let you archive these runs and compare them against each other. You can read more on the home page . This trace broadly

    Runtime & Compiler
  18. Arrays

    Creating Arrays

    Array.init generates an array of a given length by applying a function to each index of the array, starting at 0. The following line of code creates an array containing the first 5 even numbers us

    Data Structures
  19. Your First OCaml Program

    Watch Mode

    Before we dive in, note that you will typically want to use Dune's watch mode to continually compile and optionally restart your program. This ensures that the language server has the freshest poss

    First Steps
  20. Error Handling

    Inherently Unsafe Functions

    Some OCaml functions are inherently unsafe. Use them with care, not like this:

    Guides
  21. First-Class Modules

    Introduction

    Prerequisites : Modules , Functors . First-class modules let you treat modules as values. You can pass them to functions, return them from functions, and store them in data structures. This provid

    Module System
  22. Debugging

    The OCaml Debugger

    ocamldebug runs on ocamlc bytecode programs (it does not work on native code executables), and it does not work under native Windows ports of OCaml (but it runs under the Cygwin port). We now

    Guides
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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
  34. 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
  35. 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
  36. 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
  37. 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
  38. 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
  39. 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
  40. 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
  41. 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
  42. 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
  43. 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
  44. 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
  45. 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
  46. 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
  47. 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
  48. 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
  49. 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
  50. Higher Order Functions

    Sorting

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

    Introduction