I am using the functional approach to programming learn ocaml. I coded an example about graph.
In this case, I used a list to represent the an archive.
type comparison = Smaller | Equiv | Greatertype 'a archive = {archlist:'a list; comp:'a->'a->comparison}let add_to_archive arch elem = if List.exists (fun t -> arch.comp t elem == Equiv) arch.archlist then arch else { archlist = elem::arch.archlist; comp = arch.comp }let add_list_to_archive:'a archive -> 'a list -> 'a archive = fun arch elemlst -> List.fold_left (fun a t -> add_to_archive a t) arch elemlstlet make_archive comp lst = let arch = {archlist = []; comp = comp} in add_list_to_archive arch lstlet archive_member elem arch = List.exists (fun t -> arch.comp t elem = Equiv) arch.archlistlet archive_map arch_part f (arch,l) = let rec arch_map arch ll = function [] -> (arch,ll) | (c::cl) -> let ll' = select (fun c -> not (archive_member (arch_part c) arch)) (f c) in arch_map (add_list_to_archive arch (List.map arch_part ll')) (ll'@ll) cl in arch_map arch [] l
I think this implementation maybe a performance bottleneck for my case. Maybe I can use the standard library Set to implement the archive data structure. But it seems I need to specify the type of data structure in set. Is there a way I can keep the interface with archive data structure and change the implementation to Ocaml standard library Set.
Can you give an implementation to use Ocaml standard library Set to implement the archive data structure. It's okay to give just some part of the code. I will do the rest.
type comparison = Smaller | Equiv | Greatertype 'a archive = { archlist : 'a list; comp : 'a -> 'a -> comparison; }val add_to_archive : 'a archive -> 'a -> 'a archive = <fun>val add_list_to_archive : 'a archive -> 'a list -> 'a archive = <fun>val make_archive : ('a -> 'a -> comparison) -> 'a list -> 'a archive = <fun>val archive_member : 'a -> 'a archive -> bool = <fun>val archive_map : ('a -> 'b) -> ('c -> 'a list) -> 'b archive * 'c list -> 'b archive * 'a list =<fun>