1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465(** 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)letfst(a,_)=aletsnd(_,a)=aletfstll=List.mapfstlletsndll=List.mapsndl(** Helper function to unzip a list of couples *)letunzipl=ifl=[]then([],[])else(fstll,sndll)(* *)letreczipl1l2=match(l1,l2)with|([],[])->[]|(x::xs,y::ys)->(x,y)::(zipxsys)|_->failwith"lists are not of equal length"(* Search for duplicates *)letrecdup_exist=function|[]->false|hd::tl->List.exists((=)hd)tl||dup_existtl(* Search for duplicates in a list of key-value pairs *)letrecdup_key_exist=function|[]->false|(hk,_)::tl->List.exists(fun(x,_)->x=hk)tl||dup_key_existtlletrecdelete_keyksl=matchlwith|[]->[]|(k,v)::xs->ifk=ksthendelete_keyksxselse(k,v)::(delete_keyksxs)(* Search for key in a list of key-value pairs *)letreckey_existksl=matchlwith|[]->false|(k,_)::xs->ifk=ksthentrueelsekey_existksxs(* Search and get a key's value (first match) in a list of key-value pairs *)letrecget_key_valksl=matchlwith|[]->failwith"not found"|(k,v)::xs->ifk=ksthenvelseget_key_valksxsletrecfilter_by_keyskll=matchlwith|[]->[]|(k,v)::xs->ifList.memkklthen(k,v)::(filter_by_keysklxs)else(filter_by_keysklxs)