123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161(*****************************************************************************)(* *)(* MIT License *)(* Copyright (c) 2022 Nomadic Labs <contact@nomadic-labs.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)includeStdlib.ListmoduleList=Stdlib.List(* Separate the heads and the tails of a list of list.
raises if one of the lists is empty *)letget_heads_and_tailsll=letrecauxheadstails=function|[]->(revheads,revtails)|(h::t)::ll->aux(h::heads)(t::tails)ll|_->raise(Invalid_argument"get_firsts_and_tails: one list is empty")inaux[][]ll(* map for n lists of the same size grouped in ln *)(* the use of get_firsts_and_tails ensures all lists in ln have the same size *)letmapnf=function|[]->[]|ln->letrecauxln=matchhdlnwith|[]->[]|_->letheads,tails=get_heads_and_tailslninfheads::auxtailsinauxln(* List.fold_left2 which stops when end of one of the lists is reached *)letrecfold_left2_optfaccl1l2=match(l1,l2)with|[],_->acc|_,[]->acc|a1::l1,a2::l2->fold_left2_optf(facca1a2)l1l2(* List.fold_left for 3 lists of same size *)letrecfold_left3faccl1l2l3=match(l1,l2,l3)with|[],[],[]->acc|a1::l1,a2::l2,a3::l3->fold_left3f(facca1a2a3)l1l2l3|_->raise(Invalid_argument"fold_left3 : lists don’t have the same size.")(* List.fold_left for 4 lists of same size *)letrecfold_left4faccl1l2l3l4=match(l1,l2,l3,l4)with|[],[],[],[]->acc|a1::l1,a2::l2,a3::l3,a4::l4->fold_left4f(facca1a2a3a4)l1l2l3l4|_->raise(Invalid_argument"fold_left4 : lists don’t have the same size.")(* List.fold_left for 5 lists of same size *)letrecfold_left5faccl1l2l3l4l5=match(l1,l2,l3,l4,l5)with|[],[],[],[],[]->acc|a1::l1,a2::l2,a3::l3,a4::l4,a5::l5->fold_left5f(facca1a2a3a4a5)l1l2l3l4l5|_->raise(Invalid_argument"fold_left5 : lists don’t have the same size.")(* List.fold_left for 6 lists of same size *)letrecfold_left6faccl1l2l3l4l5l6=match(l1,l2,l3,l4,l5,l6)with|[],[],[],[],[],[]->acc|a1::l1,a2::l2,a3::l3,a4::l4,a5::l5,a6::l6->fold_left6f(facca1a2a3a4a5a6)l1l2l3l4l5l6|_->raise(Invalid_argument"fold_left6 : lists don’t have the same size.")letsplit_nnl=letrecauxacckl=ifk=nthen(List.revacc,l)elsematchlwith|h::t->aux(h::acc)(k+1)t|[]->raise(Invalid_argument(Printf.sprintf"split_n: n=%d >= List.length l=%d"nk))inaux[]0lletsplit_in_halfl=letlen=List.lengthlinmatchlenmod2with|0->split_n(len/2)l|_->raise(Invalid_argument(Printf.sprintf"split_in_half: length %d not even."len))letmapfl=rev(rev_mapfl)letmap2fl1l2=rev(rev_map2fl1l2)letrev_mapifl=letrecrmap_fiaccu=function|[]->accu|a::l->rmap_f(i+1)(fia::accu)linrmap_f0[]lletmapifl=rev(rev_mapifl)(* not tail-recursive *)letrecmap2_optmergel1l2=match(l1,l2)with|[],_->l2|_,[]->l1|h1::t1,h2::t2->mergeh1h2::map2_optmerget1t2(* same as List.combine but allows lists of different sizes *)letsafe_combinel1l2=letrecauxaccl1l2=match(l1,l2)with|[],_->List.revacc|_,[]->List.revacc|h1::t1,h2::t2->aux((h1,h2)::acc)t1t2inaux[]l1l2exceptionInternal(* equivalent to List.rev (List.flatten l) but tail recursive*)letrev_flattenl=letrecauxresl=matchlwith[]->res|h::t->aux(List.rev_appendhres)tinaux[]lletflattenl=List.rev@@rev_flattenl(* applies a random permutation to the elements of the given list *)letshuffle?seedl=(matchseedwithNone->()|Somei->Random.initi);List.rev_map(funx->(Random.bits(),x))l|>List.sort(fun(i,_)(j,_)->Int.compareij)|>List.rev_mapsndletsublstartlen=List.filteri(funi_->start<=i&&i<start+len)l