Created
January 3, 2025 22:18
-
-
Save chshersh/3c294739572b26212d1e8b0d8fb97c46 to your computer and use it in GitHub Desktop.
Immutable deque ipmlementation in OCaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type 'a deque = | |
| Empty | |
| Deque of 'a option * ('a * 'a) deque * 'a option | |
let rec push_front : type a . a -> a deque -> a deque = fun x deque -> | |
match deque with | |
| Empty -> Deque (Some x, Empty, None) | |
| Deque (left, child, right) -> | |
match left with | |
| None -> Deque (Some x, child, right) | |
| Some l -> Deque (None, push_front (x, l) child, right) | |
let rec pop_front : type a . a deque -> a option * a deque = function | |
| Empty -> (None, Empty) | |
| Deque (Some x, Empty, None) -> Some x, Empty | |
| Deque (Some x, child, right) -> Some x, Deque (None, child, right) | |
| Deque (None, Empty, right) -> right, Empty | |
| Deque (None, child, right) -> | |
let front, new_deque = pop_front child in | |
let first = Option.map fst front in | |
let second = Option.map snd front in | |
first, Deque (second, new_deque, right) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment