123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990(**************************************************************************)(* *)(* Typerex Libraries *)(* *)(* Copyright 2011-2017 OCamlPro SAS *)(* *)(* All rights reserved. This file is distributed under the terms of *)(* the GNU Lesser General Public License version 2.1, with the *)(* special exception on linking described in the file LICENSE. *)(* *)(**************************************************************************)letreclastlist=matchlistwith[]->raiseNot_found|[x]->x|_::tail->lasttaillet_=assert(last[1]=1);assert(last[1;2;3;4]=4);()(* Fabrice: [drop] and [take] fail when they receive a negative number.
Should they fail too when n is bigger than the list length ? Should we
provide alternatives ? *)letdropnlist=letrecauxnlist=ifn>0thenmatchlistwith[]->[]|_::tail->aux(n-1)tailelselistinifn<0theninvalid_arg"OcpList.drop";auxnlistlet_=(* assert (drop (-1) [1] = [1]); NOW FAILS *)assert(drop0[1]=[1]);assert(drop3[1;2;3;4]=[4]);assert(drop3[1;2;3]=[]);()lettakenl=letrecauxaccunl=ifn=0thenList.revaccuelsematchlwith[]->List.revaccu|h::t->aux(h::accu)(n-1)tinifn<0theninvalid_arg"OcpList.take";aux[]nllet_=assert(take0[1;2;3]=[]);assert(take1[1;2;3]=[1]);assert(take2[1;2;3]=[1;2]);assert(take3[1;2;3]=[1;2;3]);assert(take4[1;2;3]=[1;2;3]);()letmakenx=letrecauxaccunx=ifn>0thenaux(x::accu)(n-1)xelseaccuinifn<0theninvalid_arg"OcpList.make";aux[]nxlet_=assert(make01=[]);assert(make11=[1]);assert(make21=[1;1]);assert(make31=[1;1;1]);()letremovexl=List.filter((<>)x)lletremoveqxl=List.filter((!=)x)llettail_mapflist=List.rev(List.rev_mapflist)