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
22
23
24 type 'a t
25
26
27 The type of stacks containing elements of type 'a .
28
29
30
31 exception Empty
32
33
34 Raised when Stack.pop or Stack.top is applied to an empty stack.
35
36
37
38 val create : unit -> 'a t
39
40 Return a new stack, initially empty.
41
42
43
44 val push : 'a -> 'a t -> unit
45
46
47 push x s adds the element x at the top of stack s .
48
49
50
51 val pop : 'a t -> 'a
52
53
54 pop s removes and returns the topmost element in stack s , or raises
55 Stack.Empty if the stack is empty.
56
57
58
59 val pop_opt : 'a t -> 'a option
60
61
62 pop_opt s removes and returns the topmost element in stack s , or re‐
63 turns None if the stack is empty.
64
65
66 Since 4.08
67
68
69
70 val top : 'a t -> 'a
71
72
73 top s returns the topmost element in stack s , or raises Stack.Empty if
74 the stack is empty.
75
76
77
78 val top_opt : 'a t -> 'a option
79
80
81 top_opt s returns the topmost element in stack s , or None if the stack
82 is empty.
83
84
85 Since 4.08
86
87
88
89 val clear : 'a t -> unit
90
91 Discard all elements from a stack.
92
93
94
95 val copy : 'a t -> 'a t
96
97 Return a copy of the given stack.
98
99
100
101 val is_empty : 'a t -> bool
102
103 Return true if the given stack is empty, false otherwise.
104
105
106
107 val length : 'a t -> int
108
109 Return the number of elements in a stack. Time complexity O(1)
110
111
112
113 val iter : ('a -> unit) -> 'a t -> unit
114
115
116 iter f s applies f in turn to all elements of s , from the element at
117 the top of the stack to the element at the bottom of the stack. The
118 stack itself is unchanged.
119
120
121
122 val fold : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b
123
124
125 fold f accu s is (f (... (f (f accu x1) x2) ...) xn) where x1 is the
126 top of the stack, x2 the second element, and xn the bottom element. The
127 stack is unchanged.
128
129
130 Since 4.03
131
132
133
134
135 Iterators
136 val to_seq : 'a t -> 'a Seq.t
137
138 Iterate on the stack, top to bottom. It is safe to modify the stack
139 during iteration.
140
141
142 Since 4.07
143
144
145
146 val add_seq : 'a t -> 'a Seq.t -> unit
147
148 Add the elements from the iterator on the top of the stack.
149
150
151 Since 4.07
152
153
154
155 val of_seq : 'a Seq.t -> 'a t
156
157 Create a stack from the iterator
158
159
160 Since 4.07
161
162
163
164
165
166OCamldoc 2021-01-26 Stack(3)