signature Ordered = (* a totally ordered type and its compairsion functions *) sig type T val eq: T * T -> bool val lt: T * T -> bool val leq: T * T -> bool end
insert
member
functor UnbalancedSet (Element: Ordered): Set = struct type Elem = Element.T datatype Tree = E | T of Tree * Elem * Tree type Set = Tree val empty = E fun member (x, E) = false | member (x, T (a,y,b)) = if Element.lt (x,y) then member (x,a) else if Element.lt (y,x) then member (x,b) else true fun insert (x, E) = T (E, x, E) | insert (x, s as T (a, y, b)) = if Element.lt (x,y) then T (insert (x,a), y, b) else if Element.lt (y,x) then T (a, y, insert (x,b)) else s end
> checking for equality only when you hit bottom of the tree.
go E acc = elem x acc go (T a y b) acc | x < y = go a (y : acc) | otherwise = go b (y : acc)`
go E _ = False go (T a y b) acc = if x < y then go a acc else go b (y : acc) in flip go []
if Element.lt (x, y)then member (x, a, eq) else member (x, b, y)'
- hanabira 0.6.1320- + wakaba + futallaby + futaba -