Search code examples
ocamlstream-processing

Return character to stdin in Ocaml


is there an equivalent to the C++ method istream::putback(char) for OCaml?

How could I add a character to the beginning of stdin?


Solution

  • You cannot do it with an in_channel or with Stream.t. Here are some suggestions:

    1. If you are putting back a character you had read, you might want to use use peek to examine the stream instead of removing the element.

    2. You might have some luck writing a C-Interface to that function directly. I can see this being a really bad idea.

    3. Have you considered using an accumulator instead?

    4. Write a module around the current functions with a type that is a zipper or stack or some other structure that allows pushing characters back.