What does it mean. {X} in OCaml?

I'm currently trying to port some OCaml to F #. I'm β€œat the deep end” with OCaml, and my F # is a little rusty.

In any case, the OCaml code builds fine in the OCaml compiler, but (not surprisingly) gives a load of errors in the F # compiler, even with ML compatibility enabled. Some of the errors look like reserved words, but most of the errors complain about .{ In lines such as:

  m.(a).(b) <- w.{a + b * c}; 

a, b, c are integers.

I have looked many times at OCaml, Stackoverflow, the English translation of the French book O'Reilly, etc., and cannot find anything like it. Of course, this does not help that most search objects have problems with punctuation characters! Yes, I found links to . to reference records, and { } used to define records, but together? From usage, I assume this is some kind of associative or sparse array?

What does this syntax mean? What is the closest equivalent to F #?

+6
source share
1 answer

There is a pdf of oCaml documentation / manual:

http://caml.inria.fr/distrib/ocaml-3.12/ocaml-3.12-refman.pdf

Page 496 (at the bottom of the page) talks about common arrays and how to get them:

val get: (a, b, c) t β†’ int array β†’ a

Read the element of a large array. Genarray.get a [| i1; ...; iN |] returns the element a, the coordinates of which are i1 in the first dimension, i2 in the second dimension,.,., iN in the Nth dimension.

If a has a layout C, the coordinates must be greater than or equal to 0 and strictly smaller than the corresponding dimensions of a. If a is in Fortran format, the coordinates must be greater than or equal to 1 and less than or equal to the corresponding dimensions of a. raise Invalid_argument if array a does not have exactly N dimensions, or if the coordinates are outside the bounds of the array.

If N> 3, an alternative syntax is provided: you can write. {i1, i2, ..., iN} instead of Genarray.get a [| i1; ...; In |]. (Syntax a. {...} with one, two, or three coordinates is reserved for accessing one-, two-, and three-dimensional arrays, as described below.)

Further, he says (in particular, about one-dimensional arrays):

val get: (a, b, c) t β†’ int β†’ a

Array1.get ax or, alternatively, a. {x}, returns the element a from index x. x must be greater than or equal to 0 and strictly less than Array1.dim a if a has layout C. If it has the format Fortran, x must be greater than or equal to 1 and less than or equal to Array1.dim a. Otherwise, Invalid_argument is raised.

In F #, you can access the elements of an array using the Array.get method. But a stronger syntax is w.[a + b * c] . In short, in F # use [] instead of {} .

+9
source

Source: https://habr.com/ru/post/893222/


All Articles