X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=exercises%2Fch01%2Ftree.ml;h=521b6cfa3934dbc44734fef881a97101f2fea203;hb=958f72687df932030c5dfafc7066889982497729;hp=7cc7d8cc4bcf92eea8610d8f3ae20ea5f4ed92a0;hpb=f76c63f8cff09c1de6619d49e0a761e3bb6c8618;p=tiger.ml.git diff --git a/exercises/ch01/tree.ml b/exercises/ch01/tree.ml index 7cc7d8c..521b6cf 100644 --- a/exercises/ch01/tree.ml +++ b/exercises/ch01/tree.ml @@ -1,42 +1,13 @@ -module Array = ArrayLabels +module type S = sig + type ('k, 'v) t -module type TREE = sig - type 'a t + val empty : ('k, 'v) t - val empty : 'a t + val set : ('k, 'v) t -> k:'k -> v:'v -> ('k, 'v) t - val add : 'a t -> 'a -> 'a t + val get : ('k, 'v) t -> k:'k -> 'v option - val member : 'a t -> 'a -> bool -end - -module BinaryTree : TREE = struct - type 'a t = - | Node of 'a * 'a t * 'a t - | Leaf - - let empty = Leaf + val member : ('k, 'v) t -> k:'k -> bool - let rec add t x = - match t with - | Leaf -> Node (x, Leaf, Leaf) - | Node (x', left, right) when x < x' -> Node (x', add left x, right) - | Node (x', left, right) when x > x' -> Node (x', left, add right x) - | (Node _) as t' -> t' - - let rec member t x = - match t with - | Leaf -> false - | Node (x', left, _) when x < x' -> member left x - | Node (x', _, right) when x > x' -> member right x - | Node _ -> true + val to_dot : ('k, 'v) t -> k_to_string:('k -> string) -> string end - -let () = - let tree = - Array.fold_left - (Sys.argv) - ~init:BinaryTree.empty - ~f:(fun t str -> BinaryTree.add t str) - in - Printf.printf "%B\n" (BinaryTree.member tree "a")