0

https://ocaml.org/api/Stream.html

val from : (int -> 'a option) -> 'a t

Stream.from f returns a stream built from the function f. To create a new stream element, the function f is called with the current stream count. The user function f must return either Some <value> for a value or None to specify the end of the stream.

Do note that the indices passed to f may not start at 0 in the general case. For example, [< '0; '1; Stream.from f >] would call f the first time with count 2.

There are two things confusing me about this example.

1.

I had no luck googling for meaning of [< ... >] syntax. The closest I found was: https://ocaml.org/manual/lex.html#sss:keywords which just says those character sequences are keywords

[< ... ] seems to be used when printing, but not defining, polymorphic variants: https://ocaml.org/manual/polyvariant.html

If I paste in something like [< '0; '1; >] I get a syntax error.

So it's currently quite baffling to me what this example is purporting to show.

2.

The example says that [< '0; '1; Stream.from f >] would call f the first time with count 2

And I just wonder ... why? how? I can see that 2 follows on from '0 and '1, but how do those values influence starting value of f? (and why are they prefixed with '?)

Anentropic
  • 32,188
  • 12
  • 99
  • 147
  • 1
    it's the streams syntax which has been deprecated from the main compiler for years, see https://stackoverflow.com/questions/16150173/what-does-mean-in-ocaml – zehnpaard Jan 27 '22 at 17:54

1 Answers1

6

The deprecated Stream module was built to be used with a camlp{4,5} syntactic extension that added support for the [< ...>]. See https://camlp5.github.io/doc/html/parsers.html for more documentation.

However, if you are not maintaining legacy code, you probably want to avoid the Stream module altogether and use the Seq module instead. In particular, Stream will not be part of the standard library starting with OCaml 5.0 .

octachron
  • 17,178
  • 2
  • 16
  • 23