yanaijepeux/lib/stackAction.ml
Alain Emilia Anna Zscheile 414cc870c4 initial commit
2024-02-22 23:10:54 +01:00

17 lines
485 B
OCaml

(** A stack action (pop is run first, then push sequentially) *)
type 'a t =
{ pop : int
; push : 'a list
}
let merge lhs rhs =
(* merge lhs.push and rhs.pop first *)
let rec compensate push pop = match (push, pop) with
| (_, 0) -> (push, pop)
| ([], _) -> (push, pop)
| (_::xs, _) -> compensate xs (pop - 1)
in let (push, pop) = compensate lhs.push rhs.pop in
{ pop = lhs.pop + pop; push = List.append push rhs.push; }
let empty = { pop = 0; push = []; }