Commits

Maxime Buquet committed 297fa9e

Initial commit, Zip module added

Comments (0)

Files changed (2)

+syntax: glob
+
+*.cm*
+
+exception EmptyZip
+
+type 'a t = 
+    | Empty
+    | Zip of bool * 'a list * 'a * 'a list
+    (* bool: true, right / false, left *)
+
+let empty = Empty
+let is_empty z = z = Empty
+
+let pop = function
+    | Empty -> raise EmptyZip
+    | Zip (_, _, v, _) -> v
+
+let right = function
+    | Zip (_, [], v, []) -> Zip (true, [], v, [])
+    | Empty | Zip (_, _, _, []) -> raise EmptyZip
+    | Zip (b, rl, v, n :: ll) -> Zip (true, v :: rl, n, ll)
+
+let left = function
+    | Zip (_, [], v, []) -> Zip (false, [], v, [])
+    | Empty | Zip (_, [], _, _) -> raise EmptyZip
+    | Zip (b, n :: rl, v, ll) -> Zip (false, rl, n, v :: ll)
+
+let push x = function
+    | Empty -> Zip (true, [], x, [])
+    | Zip (b, rl, v, ll) when b = true ->
+            Zip (true, v :: rl, x, ll)
+    | Zip (_, rl, v, ll) ->
+            Zip (false, rl, x, v :: ll)