Module Stack
: sigend
Last-in first-out stacks.
This module implements stacks (LIFOs), with in-place modification.
Alertunsynchronized_access. Unsynchronized accesses to stacks are a programming error.
Unsynchronized accesses
Unsynchronized accesses to a stack may lead to an invalid queue state. Thus, concurrent accesses to
stacks must be synchronized (for instance with a Mutex.t ).
type!'at
The type of stacks containing elements of type 'a .
exceptionEmpty
Raised when Stack.pop or Stack.top is applied to an empty stack.
valcreate : unit->'at
Return a new stack, initially empty.
valpush : 'a->'at->unitpushxs adds the element x at the top of stack s .
valpop : 'at->'apops removes and returns the topmost element in stack s , or raises Stack.Empty if the stack is empty.
valpop_opt : 'at->'aoptionpop_opts removes and returns the topmost element in stack s , or returns None if the stack is empty.
Since 4.08
valdrop : 'at->unitdrops removes the topmost element in stack s , or raises Stack.Empty if the stack is empty.
Since 5.1
valtop : 'at->'atops returns the topmost element in stack s , or raises Stack.Empty if the stack is empty.
valtop_opt : 'at->'aoptiontop_opts returns the topmost element in stack s , or None if the stack is empty.
Since 4.08
valclear : 'at->unit
Discard all elements from a stack.
valcopy : 'at->'at
Return a copy of the given stack.
valis_empty : 'at->bool
Return true if the given stack is empty, false otherwise.
vallength : 'at->int
Return the number of elements in a stack. Time complexity O(1)
valiter : ('a->unit)->'at->unititerfs applies f in turn to all elements of s , from the element at the top of the stack to the element
at the bottom of the stack. The stack itself is unchanged.
valfold : ('acc->'a->'acc)->'acc->'at->'accfoldfaccus is (f(...(f(faccux1)x2)...)xn) where x1 is the top of the stack, x2 the second
element, and xn the bottom element. The stack is unchanged.
Since 4.03
StacksandSequencesvalto_seq : 'at->'aSeq.t
Iterate on the stack, top to bottom. It is safe to modify the stack during iteration.
Since 4.07
valadd_seq : 'at->'aSeq.t->unit
Add the elements from the sequence on the top of the stack.
Since 4.07
valof_seq : 'aSeq.t->'at
Create a stack from the sequence.
Since 4.07
OCamldoc 2025-06-12 Stack(3o)