123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990openTypesmoduleDict=struct(** Search for duplicates *)letrecdup_exists=function|[]->false|hd::tl->List.exists((=)hd)tl||dup_existstl(** Generate an empty dictionary *)letempty:unit->env_type=fun_->[](** Search for duplicates in a list of key-value pairs *)(* let rec dup_exists = function
| [] -> false
| (hk, _)::tl -> List.exists (fun (x,_) -> x = hk) tl || dup_exists tl *)(** Delete all occurrences of a key in a dictionary *)letrecdeleteksl=matchlwith|[]->[]|(k,v)::xs->ifk=ksthendeleteksxselse(k,v)::(deleteksxs)(** Insert a value into a key-value dictionary *)letinsertdkeyvalue=(key,value)::(deletekeyd)(** Check if a key exists in a list of key-value pairs *)letrecexistsksl=matchlwith|[]->false|(k,_)::xs->ifk=ksthentrueelseexistsksxs(** Search and Dict.get a key's value (first match) in a list of key-value pairs *)letrecgetksl=matchlwith|[]->failwith"not found"|(k,v)::xs->ifk=ksthenvelsegetksxsletrecfilterkll=matchlwith|[]->[]|(k,v)::xs->ifList.memkklthen(k,v)::(filterklxs)else(filterklxs)(** Insert a list of keys and a list of values in a dictionary *)letrecinsertmanyenvident_listvalue_list=match(ident_list,value_list)with|([],[])->env|(i::ident_rest,v::value_rest)->insertmany(insertenviv)ident_restvalue_rest|_->failwith"lists are not of equal length"end(** Helper function to take the first elements of a list *)letrectakekxs=matchkwith|0->[]|k->matchxswith|[]->failwith"take"|y::ys->y::(take(k-1)ys)(** Helper function to drop the first elements of a list *)letrecdropkxs=matchkwith|0->xs|k->matchxswith|[]->failwith"drop"|_::ys->(drop(k-1)ys)letlastl=List.hd(drop((List.lengthl)-1)l)letfst(a,_)=aletsnd(_,a)=aletfstll=List.mapfstlletsndll=List.mapsndl(** Helper function to unzip a list of couples *)letunzipl=ifl=[]then([],[])else(fstll,sndll)(* Zip together two lists with in single list of couples *)letreczipl1l2=match(l1,l2)with|([],[])->[]|(x::xs,y::ys)->(x,y)::(zipxsys)|_->failwith"lists are not of equal length"