Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- signature HASHABLE =
- sig
- type t
- val hash : t -> int
- end
- signature HASH_TABLE =
- sig
- structure Key : HASHABLE
- type 'v table = (Key.t * 'v) list ref (* for default's sake *)
- val newTable : unit -> 'v table
- val insert : 'v table -> Key.t -> 'v -> unit
- val apply : 'v table -> (Key.t * 'v -> unit) -> unit
- end
- structure Key1 =
- struct
- type t = int
- val hash = fn x => x
- end
- structure Key2 =
- struct
- type t = int
- val hash = fn x => x div 2 (* the only change *)
- end
- structure H1 : HASH_TABLE =
- struct
- structure Key = Key1
- type 'v table = (Key.t * 'v) list ref
- fun newTable () = ref []
- fun insert t k v =
- if List.exists (fn (x,y) => (Key.hash x) = (Key.hash k)) (!t)
- then t := (map (fn (x,y) =>
- if (Key.hash x) = (Key.hash k) then (x,v) else (x,y)) (!t))
- else t := ((k,v) :: (!t))
- fun apply ht f = List.app f (! ht)
- end
- structure H2 : HASH_TABLE =
- struct
- structure Key = Key2
- type 'v table = (Key.t * 'v) list ref
- fun newTable () = ref []
- fun insert t k v =
- if List.exists (fn (x,y) => (Key.hash x) = (Key.hash k)) (!t)
- then t := (map (fn (x,y) =>
- if (Key.hash x) = (Key.hash k) then (x,v) else (x,y)) (!t))
- else t := ((k,v) :: (!t))
- fun apply ht f = List.app f (! ht)
- end
- functor MainFn (A : sig
- structure HT1 : HASH_TABLE where type Key.t = int
- structure HT2 : HASH_TABLE where type Key.t = int
- end) =
- struct
- open A
- fun foo () =
- let
- val newt = HT1.newTable ()
- val () = HT1.insert newt 3 "hi"
- val () = HT2.insert newt 3 "ho"
- val () =
- H1.apply newt (fn (k,v) => print ((Int.toString k) ^ " |-> " ^ v ^ "\n"))
- in
- newt (* only contains (3, "ho") ! *)
- end
- end
- structure Main = MainFn (struct structure HT1 = H1 structure HT2 = H2 end)
Add Comment
Please, Sign In to add comment