2022-01-05 06:51:48 +00:00
|
|
|
open FromStdlib open Exposed
|
2021-12-16 10:01:08 +00:00
|
|
|
|
|
|
|
let pop (st : 'a stack) : 'a option * 'a stack =
|
|
|
|
match st with
|
|
|
|
| Empty -> (None, Empty)
|
|
|
|
| Stacked (x, xs) -> (Some x, xs)
|
|
|
|
|
|
|
|
let push (a : 'a) (st : 'a stack) : 'a stack =
|
|
|
|
Stacked (a, st)
|
|
|
|
|
|
|
|
let peek (st : 'a stack) : 'a option =
|
|
|
|
match st with
|
|
|
|
| Empty -> None
|
|
|
|
| Stacked (x, xs) -> Some x
|
|
|
|
|
|
|
|
let empty : 'a stack = Empty
|
|
|
|
|
|
|
|
let rec height_helper (height : int) (st : 'a stack) =
|
|
|
|
match st with
|
|
|
|
| Empty -> height
|
2021-12-18 09:20:30 +00:00
|
|
|
| Stacked (x, xs) -> height_helper (Int.(height + 1)) xs
|
2021-12-16 10:01:08 +00:00
|
|
|
|
|
|
|
let height (st : 'a stack) : int =
|
|
|
|
height_helper 0 st
|
|
|
|
|
|
|
|
let is_empty (st : 'a stack) =
|
|
|
|
match st with
|
|
|
|
| Empty -> true
|
|
|
|
| Stacked _ -> false
|