(* simple HOF dictionary *)
exception NotFound;
(* empty dictionary *)
fun null_dict(key:'a) = raise NotFound;
(*
* update receives an entry (key, data),
* an old dictionary (dict) and
* returns a new dictionary (dict')
*)
fun update (key:''a, data:'b) (dict:''a -> 'b) =
let fun dict' (k':''a) =
if (k' = key)
then data (* this is the entry we were looking for *)
else dict(k') (* it was not, look further down in the old dict *)
in dict'
end;
(* adds a new entry that raises "NotFound for the given key *)
fun delete (key:''a) dict =
fn (k') => if (k' = key)
then raise NotFound
else dict(k');
(* try it *)
(* create 2 entries *)
val dict1 = (update ("pi", 3.141)
(update ("e", 2.718) null_dict));
(* get some entries *)
dict1("pi");
dict1("e");
(* add another entry *)
val dict2 = update ("one", 1.0) dict1;
dict2("one");
dict2("pi");
(* dict2("cat"); *)
val dict2 = delete "pi" dict2;
(*dict2("pi");*)
dict2("one");
               (
geocities.com/htrefftz/mum/cs505)                   (
geocities.com/htrefftz/mum)                   (
geocities.com/htrefftz)