715 search results for ""

Showing 501 - 550
  1. Functors

    Dependencies Between Modules

    Check the program's behaviour using opam exec -- dune exec funkt < dune . Module List through List.iter and f 's type Module Out_channel through Out_channel.output_string It embeds an addit

    Module System
  2. Memoization

    Fibonacci

    Although this code uses imperative constructs (specifically, array update), the side effects are not visible outside the function fibm . So from a client's perspective, fibm is functional. The

    Data Structures
  3. Sequences

    Sequences for Conversions

    Similar functions are also provided for sets, maps, hash tables ( Hashtbl ), and others. When implementing a datatype module, it is advised to expose to_seq and of_seq functions. Lists Ar

    Data Structures
  4. Lists

    Higher Order Functions on Lists

    Notice the type of the function f in parentheses as part of the whole type. This map function, given a function of type 'a -> 'b and a list of 'a s, will build a list of 'b' s. Sometime

    Introduction
  5. Basic Data Types and Pattern Matching

    Strings

    Operations on string values are provided by the Stdlib and the String modules. Indexed access to string characters is possible using the following syntax: <!--CR I'm not sure how to rearran

    Introduction
  6. OCaml Programming Guidelines

    Opening modules

    <!-- $MDX skip --> Justification : The use of unqualified identifiers is ambiguous and gives rise to difficult-to-detect semantic errors. Avoid open directives, using instead the qualified

    Resources
  7. Mutability and Imperative Control Flow

    Conclusion

    A mutable state is neither good nor bad. For the cases where a mutable state enables a significantly simpler implementation, OCaml provides fine tools to deal with it. We looked at references, mutabl

    Introduction
  8. Mutability and Imperative Control Flow

    While Loop

    In this example, the while loop continues to execute as long as the value held by the reference i is less than 5 . The iteration executes the body expression as long as the condition remains tr

    Introduction
  9. Lists

    Lists and Tail Recursion

    In the standard library documentation, functions which are not tail-recursive are marked. Or, we can do it all in one function: We call such a function tail-recursive . We may write a wrappe

    Introduction
  10. Options

    Map Over an Option

    Here, map is applied to a list inside Some , doubling each number. If None , the list transformation doesn't occur. Example 3 : Doubling Each Element in an Option of a List If there is a st

    Data Structures
  11. OCaml Programming Guidelines

    How to Write Lists

    Write x :: l with spaces around the :: (since :: is an infix operator, hence surrounded by spaces) and [1; 2; 3] (since ; is a delimiter, hence followed by a space).

    Resources
  12. Basic Data Types and Pattern Matching

    Booleans

    Conditional expression and pattern matching on a Boolean are the same: The test subexpression must have type bool . Branches subexpressions must have the same type. In OCaml, if … then …

    Introduction
  13. Basic Data Types and Pattern Matching

    Lists

    In the above expressions, [1; 2; 3] is the value that is matched over. Each expression between the | and -> symbols is a pattern. They are expressions of type list , only formed using [] , :

    Introduction
  14. Hash Tables

    Replacing Data in Hash Tables

    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 replace the previous ones. In this case, one uses Hashtbl.replace

    Data Structures
  15. Basic Data Types and Pattern Matching

    Arrays

    Operations on arrays are provided by the Array module. There is a dedicated tutorial on Arrays . The left-arrow <- is the array update operator. Above, it means the cell at index 2 is set to va

    Introduction
  16. Higher Order Functions

    Currying

    But we can also curry our reveal function to take 2 arguments! If we wanted to use reveal on a name, we have to put it into a tuple, and then do the call. Like this: Take for example this

    Introduction
  17. Preprocessors and PPXs

    Using PPXs

    And that's all you need to use PPXs! Although these instructions will work for most PPXs, note that the first source of information will be the package documentation, as some PPXs might need some

    Advanced Topics
  18. OCaml Programming Guidelines

    How Much to Indent

    The change in indentation between successive lines of the program is generally 1 or 2 spaces. Pick an amount to indent and stick with it throughout the program.

    Resources
  19. Options

    Exceptions vs Options

    See Error Handling for a longer discussion on error handling using options, exceptions, and other means. The function Sys.getenv : string -> string from the OCaml standard library queries the

    Data Structures
  20. Debugging

    Limitations

    Compiler builtins are described in the standard library documentation. At compile time, the __LOC__ builtin is substituted with its location in the program, described as a string "File %S, l

    Guides
  21. A Tour of OCaml

    Pairs and Tuples

    The type of tuples is written using * between the components' types. Note: The function snd is predefined in the OCaml standard library. Access to the components of tuples is done using patte

    First Steps
  22. Formatting and Wrapping Text

    Boxes

    within a “h” box: within a “v” box: within a “hv” box: If there is enough room to print the box on the line: But "---b---b---" that cannot fit on the line is written within a

    Tutorials
  23. Higher Order Functions

    Mapping Lists

    Note how we use the :: constructor to both deconstruct the list and reconstruct it. The main difference is that instead of throwing away the resulting value from running our function over the el

    Introduction
  24. Introduction to opam Switches

    Removing a Switch

    Learn more about opam switches in the opam manual and the opam-switch reference . For a local switch, use the path: To delete a switch you no longer need:

    Tooling
  25. Values and Functions

    Pattern Matching on Records

    We can pattern match on records: <!--Because records are implicitly single-constructor variants,-->

    Introduction
  26. Profiling

    Profiling Tools

    After running the program as normal, the profiling code dumps out a file gmon.out which we can interpret with gprof : We compile it using the -p option to ocamlopt which tells the compil

    Guides
  27. Debugging

    Tracing Functions Calls in the Toplevel

    The simplest way to debug programs in the toplevel is to follow the function calls, by “tracing” the faulty function:

    Guides
  28. Debugging

    Getting Help and Info in the Debugger

    To get more info about the current status of the debugger you can ask it directly at the toplevel prompt of the debugger; for instance:

    Guides
  29. A Tour of OCaml

    Using the result Type

    So one may write: Another way to deal with errors in OCaml is by returning a value of type result , which can represent either the correct result or an error. Here is how it is defined:

    First Steps
  30. Debugging

    Launching the Debugger

    Then the debugger answers with a banner and a prompt: We launch the debugger: At runtime, the program raises an uncaught exception Not_found . Suppose we want to find where and why this e

    Guides
  31. OCaml Programming Guidelines

    Width of the Page

    Justification : This width makes it possible to read the code on all displays and to print it in a legible font on a standard sheet. The page is 80 columns wide.

    Resources
  32. OCaml Docker Images

    OCaml Compiler Variants

    Flambda enabled ( ocaml-option-flambda ) Frame pointers enabled ( ocaml-option-fp ) Beyond standard OCaml versions, there are variants like:

    Additional Tooling
  33. OCaml Docker Images

    How They're Built

    The images are updated weekly via an automated pipeline at images.ci.ocaml.org . The images are built by an OCurrent pipeline that produces Docker images for various combinations of Linux distrib

    Additional Tooling
  34. Command-line Arguments

    Sys.argv

    <!-- $MDX dir=examples --> Note that ocaml launched a subprocess that actually runs the program where argv is args.ml arg1 arg2 arg3 . You can also compile your program using ocamlopt -o ar

    Tutorials
  35. Fix Homebrew Errors on Apple M1

    Install CLT

    If they're not installed, let's install them now. You don't have to install all of XCode; you can install just the CLT by downloading them directly from Apple's Developer . Look for a non-beta versi

    Resources
  36. Profiling

    Further Reading

    <!--### Java dynamic dispatch **There are some serious mistakes in the last paragraph:** * Dynamic method dispatch itself is seldom a performance problem. In languages without multiple inheritance

    Guides
  37. Sequences

    Fibs with Seq.Cons

    <!-- Or with an int version ``` ocaml # ints_v2 1 |> Seq.take 10 |> List.of_seq;; - : int list = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10] ``` --> This implementation successfully defines a producer of laz

    Data Structures
  38. Values and Functions

    Introduction

    We use UTop to understand these concepts by example. You are encouraged to modify the examples to gain a better understanding. In OCaml, functions are treated as values, so you can use functions as

    Introduction
  39. Installing OCaml

    Install opam

    To install opam, you can use your system package manager or download the binary distribution . The details are available in these links, but for convenience, we use package distributions: Opam al

    First Steps
  40. OCaml Docker Images

    Supported Architectures

    The images are multi-arch, with support for arm , x86 , and ppc architectures in both 32-bit and 64-bit variants where possible.

    Additional Tooling
  41. Operators

    Binary Operator

    Don't define wide scope operators. Restrict their scope to module or function. Don't use many of them. Before defining a custom binary operator, check that the symbol is not already used. This can be

    Advanced Topics
  42. Options

    Fold an Option

    This function safely handles optional names by providing a fallback greeting: Example 3 : Handling Optional User Input Since None is given, it returns the default 0 . Here, Option.fold ap

    Data Structures
  43. Maps

    Changing the Value Associated With a Key

    You should experiment with different update functions; several behaviors are possible. To change a key's associated value, use the update function. It takes a key, a map, and an update functio

    Data Structures
  44. OCaml Programming Guidelines

    Iterators

    In case of express need, be sure to add an explanatory comment. In my opinion, it's absolutely necessary! <!-- $MDX skip --> even though you get: <!-- $MDX skip --> On the other hand, avoid w

    Resources
  45. OCaml Programming Guidelines

    How to Compile

    The make utility is indispensable for managing the compilation and recompilation of programs. Sample make files can be found on The Hump . You can also consult the Makefiles for the OCam

    Resources
  46. Memory Representation of Values

    Conclusion

    Understanding the Garbage Collector Calling C Libraries Other recommended tutorials: We covered the precise mapping from OCaml types to their internal runtime representation in memory, which shou

    Runtime & Compiler
  47. Profiling

    Tail Recursion

    So that's pretty conclusive. Calling Tail__loop_56 will first print the string, and then jump back to the top, then print the string, and jump back, and so on forever. It's a simple loop, not

    Guides
  48. Values and Functions

    Pattern Matching on User-Defined Types

    This also works with user-defined types.

    Introduction
  49. Values and Functions

    Closures

    Inside the max_42 function, the environment contains an additional binding between the first parameter of max and the value 42. Partially applying arguments to a function also creates a new clo

    Introduction
  50. Hash Tables

    Introduction

    A hash table data structure achieves efficient reads and writes by employing a hashing function that converts the key of a key/value pair into an algorithmically unique "fingerprint" known as a h

    Data Structures