678 search results for ""

Showing 551 - 600
  1. Basic Data Types and Pattern Matching

    Results

    Operations on results are provided by the Result module. Results are discussed in the Error Handling guide. The result type can be used to express that a function's outcome can be either succ

    Introduction
  2. File Manipulation

    Gotchas

    Don't forget to flush your out_channel s if you want to actually write something. This is particularly important if you are writing to non-files such as the standard output ( stdout ) or a socke

    Tutorials
  3. OCaml on Windows

    Vim and Emacs

    If you use Vim , the default Cygwin Vim will not work with Merlin. You will need to install Vim separately. In addition to the usual instructions printed when installing Merlin, you may need to

    Resources
  4. Mutability and Imperative Control Flow

    Arrays

    For a more detailed discussion on arrays, see the Arrays tutorial. the array location to update (when on the left-hand side of <- ), or the cell's content (when on the right-hand side of <- ). T

    Introduction
  5. File Manipulation

    Buffered Channels

    channels that write to a file: type out_channel channels that read from a file: type in_channel The normal way of opening a file in OCaml returns a channel . There are two kinds of channels:

    Tutorials
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. Installing OCaml

    For Linux

    Note : The Debian package for opam, which is also used in Ubuntu, has the OCaml compiler as a recommended dependency. By default, such dependencies are installed. If you want to only install opam w

    First Steps
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. Error Handling

    Inherently Unsafe Functions

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

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