1Stack(3) OCaml library Stack(3)
2
3
4
6 Stack - Last-in first-out stacks.
7
9 Module Stack
10
12 Module Stack
13 : sig end
14
15
16 Last-in first-out stacks.
17
18 This module implements stacks (LIFOs), with in-place modification.
19
20
21 Alert unsynchronized_accesses. Unsynchronized accesses to stacks are a
22 programming error.
23
24
25
26
27
28
29
30 Unsynchronized accesses
31
32 Unsynchronized accesses to a stack may lead to an invalid queue state.
33 Thus, concurrent accesses to stacks must be synchronized (for instance
34 with a Mutex.t ).
35
36 type 'a t
37
38
39 The type of stacks containing elements of type 'a .
40
41
42
43 exception Empty
44
45
46 Raised when Stack.pop or Stack.top is applied to an empty stack.
47
48
49
50 val create : unit -> 'a t
51
52 Return a new stack, initially empty.
53
54
55
56 val push : 'a -> 'a t -> unit
57
58
59 push x s adds the element x at the top of stack s .
60
61
62
63 val pop : 'a t -> 'a
64
65
66 pop s removes and returns the topmost element in stack s , or raises
67 Stack.Empty if the stack is empty.
68
69
70
71 val pop_opt : 'a t -> 'a option
72
73
74 pop_opt s removes and returns the topmost element in stack s , or re‐
75 turns None if the stack is empty.
76
77
78 Since 4.08
79
80
81
82 val top : 'a t -> 'a
83
84
85 top s returns the topmost element in stack s , or raises Stack.Empty if
86 the stack is empty.
87
88
89
90 val top_opt : 'a t -> 'a option
91
92
93 top_opt s returns the topmost element in stack s , or None if the stack
94 is empty.
95
96
97 Since 4.08
98
99
100
101 val clear : 'a t -> unit
102
103 Discard all elements from a stack.
104
105
106
107 val copy : 'a t -> 'a t
108
109 Return a copy of the given stack.
110
111
112
113 val is_empty : 'a t -> bool
114
115 Return true if the given stack is empty, false otherwise.
116
117
118
119 val length : 'a t -> int
120
121 Return the number of elements in a stack. Time complexity O(1)
122
123
124
125 val iter : ('a -> unit) -> 'a t -> unit
126
127
128 iter f s applies f in turn to all elements of s , from the element at
129 the top of the stack to the element at the bottom of the stack. The
130 stack itself is unchanged.
131
132
133
134 val fold : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b
135
136
137 fold f accu s is (f (... (f (f accu x1) x2) ...) xn) where x1 is the
138 top of the stack, x2 the second element, and xn the bottom element. The
139 stack is unchanged.
140
141
142 Since 4.03
143
144
145
146
147 Stacks and Sequences
148 val to_seq : 'a t -> 'a Seq.t
149
150 Iterate on the stack, top to bottom. It is safe to modify the stack
151 during iteration.
152
153
154 Since 4.07
155
156
157
158 val add_seq : 'a t -> 'a Seq.t -> unit
159
160 Add the elements from the sequence on the top of the stack.
161
162
163 Since 4.07
164
165
166
167 val of_seq : 'a Seq.t -> 'a t
168
169 Create a stack from the sequence.
170
171
172 Since 4.07
173
174
175
176
177
178OCamldoc 2023-07-20 Stack(3)