Download OCaml 4 Reference Guide

Transcript
422
Predefined parsers
val next : 'a t -> 'a
Return the first element of the stream and remove it from the stream. Raise Stream.Failure
if the stream is empty.
val empty : 'a t -> unit
Return () if the stream is empty, else raise Stream.Failure.
Useful functions
val peek : 'a t -> 'a option
Return Some of ”the first element” of the stream, or None if the stream is empty.
val junk : 'a t -> unit
Remove the first element of the stream, possibly unfreezing it before.
val count : 'a t -> int
Return the current count of the stream elements, i.e. the number of the stream elements
discarded.
val npeek : int -> 'a t -> 'a list
npeek n returns the list of the n first elements of the stream, or all its remaining elements if
less than n elements are available.
21.34
Module String : String operations.
Given a string s of length l, we call character number in s the index of a character in s. Indexes
start at 0, and we will call a character number valid in s if it falls within the range [0...l-1]. A
position is the point between two characters or at the beginning or end of the string. We call a
position valid in s if it falls within the range [0...l]. Note that character number n is between
positions n and n+1.
Two parameters start and len are said to designate a valid substring of s if len >= 0 and
start and start+len are valid positions in s.
OCaml strings can be modified in place, for instance via the String.set[21.34] and
String.blit[21.34] functions described below. This possibility should be used rarely and with
much care, however, since both the OCaml compiler and most OCaml libraries share strings as if
they were immutable, rather than copying them. In particular, string literals are shared: a single
copy of the string is created at program loading time and returned by all evaluations of the string
literal. Consider for example:
# let f () = "foo";;
val f : unit -> string = <fun>