(define si (set (lambda (i j) (if (< i j) -1 (if (> i j) 1 0))))) (apply (lambda (empty empty? mem add singleton remove union inter diff compare equal subset iter fold for_all exists filter partition cardinal elements min_elt max_elt choose split) (let ((pr (lambda (s) (display s) (display " "))) (cmpn (lambda s (let ac ((t s)) (if (null? t) empty (add (car t) (ac (cdr t)))))))) ;(add 42 (add 42 empty)) ;(iter pr (add 38 (add 42 empty))) ;(remove 42 (add 38 (add 42 empty))) ;(iter pr (diff (singleton 11) (singleton 41))) ;(iter pr (diff (cmpn 3 6 4 8 33) (cmpn 8 6 33))) ;(iter pr (cmpn 4 5 3 5 6)) ;(cmpn 5 3 5 6) ;(iter pr (add 4 (cmpn 5 3 5 6))) ;(union (cmpn 20 24 33 11 5) (cmpn 24 11 36)) ;(iter pr (union (cmpn 20 24 33 11 5) (cmpn 24 11 36))) ;(iter pr (inter (cmpn 20 24 33 11 5) (cmpn 24 11 36))) ;(elements (cmpn 20 24 33 11 5)) ;(fold + empty 3) (fold + (cmpn 20 24 33 11 5) 3) ;(split 23 (cmpn 20 24 33 11 5)) ;(compare (cmpn 20 24 33 11 5) (cmpn 33 24 11 5 20)) ;(fold cons (cmpn 20 24 33 11 5) ()) ;(equal (cmpn 20 24 33 12 5) (cmpn 33 24 12 5 20)) ;(iter pr (filter odd? (cmpn 20 24 33 12 5))) ;(for_all odd? (cmpn 20 24 33 12 5)) ;(exists negative? (cmpn 20 24 33 12 5)) ;(partition odd? (cmpn 5)) ;(partition negative? (cmpn 20 24 33 12 5)) ;(let ((a (partition odd? (cmpn 20 24 33 12 5)))) (cons (fold cons (car a) ()) (fold cons (cdr a) ()))) ;(min_elt (cmpn 20 24 33 12 5)) ;(mem 69 (cmpn 20 24 33 12 5)) )) si) (* ingest stuff from /usr/local/lib/ocaml/set.ml *) module Si = struct type t = int let compare x y = if x < y then -1 else if y < x then 1 else 0 end;; module SS = Make(Si);; (* type enumeration = Make(Si).enumeration = End | More of elt * t * enumeration *) let rec pe = function SS.End -> [] | SS.More (el, t, e) -> el :: (pe e);; (* val pe : SS.enumeration -> SS.elt list = *) # SS.cons_enum SS.empty SS.End;; - : SS.enumeration = SS.End # SS.cons_enum (SS.singleton 7) SS.End;; - : SS.enumeration = SS.More (7, SS.Empty, SS.End) let rec cmpn = function [] -> SS.empty | a::b -> SS.add a (cmpn b);; let s = cmpn [5; 3; 5; 6];; (* s = SS.Node (SS.Node (SS.Node (SS.Empty, 3, SS.Empty, 1), 5, SS.Empty, 2), 6, SS.Empty, 3) *) SS.add 4 s;; (* SS.Node (SS.Node (SS.Empty, 3, SS.Node (SS.Empty, 4, SS.Empty, 1), 2), 5, SS.Node (SS.Empty, 6, SS.Empty, 1), 3) *) type enumeration = Make(Si).enumeration = End | More of elt * t * enumeration