123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469includeSeq(*
(*
include Seq
*)
type 'a t = unit -> 'a node
and 'a node =
'a Stdcompat__init.seq_node =
| Nil
| Cons of 'a * 'a t
let empty () = Nil
let return x () = Cons (x, empty)
let rec map f seq () = match seq () with
| Nil -> Nil
| Cons (x, next) -> Cons (f x, map f next)
let rec filter_map f seq () = match seq () with
| Nil -> Nil
| Cons (x, next) ->
match f x with
| None -> filter_map f next ()
| Some y -> Cons (y, filter_map f next)
let rec filter f seq () = match seq () with
| Nil -> Nil
| Cons (x, next) ->
if f x
then Cons (x, filter f next)
else filter f next ()
let rec flat_map f seq () = match seq () with
| Nil -> Nil
| Cons (x, next) ->
flat_map_app f (f x) next ()
and flat_map_app f seq tail () = match seq () with
| Nil -> flat_map f tail ()
| Cons (x, next) ->
Cons (x, flat_map_app f next tail)
let fold_left f acc seq =
let rec aux f acc seq = match seq () with
| Nil -> acc
| Cons (x, next) ->
let acc = f acc x in
aux f acc next
in
aux f acc seq
let iter f seq =
let rec aux seq = match seq () with
| Nil -> ()
| Cons (x, next) ->
f x;
aux next
in
aux seq
*)letconsxseq()=Cons(x,seq)letrecappendab()=matcha()with|Nil->b()|Cons(hd,tl)->Cons(hd,appendtlb)letrecunfoldfstate()=matchfstatewith|None->Nil|Some(value,state)->Cons(value,unfoldfstate)letconcat_map=flat_mapletrecconcatseq()=matchseq()with|Nil->Nil|Cons(hd,tl)->appendhd(concattl)()(*
(* Temporary reimplemented here for compatibility with alpha releases. *)
let fold_lefti f acc seq =
let rec aux f acc i seq = match seq () with
| Nil -> acc
| Cons (x, next) ->
let acc = f acc i x in
aux f acc (succ i) next
in
aux f acc 0 seq
*)letis_emptyseq=matchseq()with|Nil->true|Cons_->falseletunconsseq=matchseq()with|Nil->None|Cons(hd,tl)->Some(hd,tl)letreclength_recaccuseq=matchseq()with|Nil->accu|Cons(_hd,tl)->length_rec(succaccu)tlletlengthseq=length_rec0seqletiterifseq=letrecauxiseq=matchseq()with|Nil->()|Cons(x,next)->fix;aux(succi)nextinaux0seqletfold_leftifaccseq=letrecauxfacciseq=matchseq()with|Nil->acc|Cons(x,next)->letacc=faccixinauxfacc(succi)nextinauxfacc0seqletrecfor_allpseq=matchseq()with|Nil->true|Cons(hd,tl)->phd&&for_allptlletrecexistspseq=matchseq()with|Nil->false|Cons(hd,tl)->phd||existsptlletrecfindpseq=matchseq()with|Nil->None|Cons(hd,tl)->ifphdthenSomehdelsefindptlletrecfind_mapfseq=matchseq()with|Nil->None|Cons(hd,tl)->matchfhdwith|None->find_mapftl|Some_asresult->resultletiter2fab=letrecauxab=matcha()with|Nil->()|Cons(a_hd,a_tl)->matchb()with|Nil->()|Cons(b_hd,b_tl)->fa_hdb_hd;auxa_tlb_tlinauxabletfold_left2faccab=letrecauxaccab=matcha()with|Nil->acc|Cons(a_hd,a_tl)->matchb()with|Nil->acc|Cons(b_hd,b_tl)->aux(facca_hdb_hd)a_tlb_tlinauxaccabletrecfor_all2pab=matcha()with|Nil->true|Cons(a_hd,a_tl)->matchb()with|Nil->true|Cons(b_hd,b_tl)->pa_hdb_hd&&for_all2pa_tlb_tlletrecexists2pab=matcha()with|Nil->false|Cons(a_hd,a_tl)->matchb()with|Nil->false|Cons(b_hd,b_tl)->pa_hdb_hd||exists2pa_tlb_tlletrecequalpab=matcha(),b()with|Nil,Nil->true|Nil,Cons_|Cons_,Nil->false|Cons(a_hd,a_tl),Cons(b_hd,b_tl)->pa_hdb_hd&&equalpa_tlb_tlletreccompareoab=matcha(),b()with|Nil,Nil->0|Nil,Cons_->-1|Cons_,Nil->1|Cons(a_hd,a_tl),Cons(b_hd,b_tl)->matchoa_hdb_hdwith|0->compareoa_tlb_tl|result->resultletinitnf=letrecauxi()=ifi<nthenCons(fi,aux(succi))elseNilinifn<0theninvalid_arg"Seq.init: length should be non-negative";aux0letrecrepeatx()=Cons(x,repeatx)letrecforevergen()=Cons(gen(),forevergen)letcycleseq()=matchseq()with|Nil->Nil|Cons(hd,tl)->letrecauxtl'()=matchtl'()with|Nil->Cons(hd,auxtl)|Cons(hd',tl')->Cons(hd',auxtl')inCons(hd,auxtl)letreciterate1fx()=letfx=fxinCons(fx,iterate1ffx)letiteratefx()=Cons(x,iterate1fx)letmapifseq=letrecauxiseq()=matchseq()with|Nil->Nil|Cons(x,next)->Cons(fix,aux(succi)next)inaux0seqletscanfaccseq=letrecauxfaccseq()=matchseq()with|Nil->Nil|Cons(x,next)->letacc=faccxinCons(acc,auxfaccnext)inconsacc(auxfaccseq)letrectake_recnseq=ifn>0thenfun()->matchseq()with|Nil->Nil|Cons(hd,tl)->Cons(hd,take_rec(predn)tl)elseemptylettakenseq=ifn<0theninvalid_arg"Seq.take: length should be non-negative";take_recnseqletrecdrop_recnseq=matchseq()with|Nil->empty|Cons(_hd,tl)->letn'=predninifn'>0thendrop_recn'tlelsetlletdropnseq=ifn<0theninvalid_arg"Seq.drop: length should be non-negative";ifn=0thenseqelsedrop_recnseqletrectake_whilepseq()=matchseq()with|Nil->Nil|Cons(hd,tl)->ifphdthenCons(hd,take_whileptl)elseNilletrecdrop_while_recpseq=matchseq()with|Nil->Nil|Cons(hd,tl)asresult->ifphdthendrop_while_recptlelseresultletdrop_whilepseq()=drop_while_recpseqletrecgroupeqseq()=matchseq()with|Nil->Nil|Cons(hd,tl)->Cons(conshd(take_while(eqhd)tl),groupeq(drop_while(eqhd)tl))letrecmemoizeseq=letnext=lazy(matchseq()with|Nil->Nil|Cons(hd,tl)->Cons(hd,memoizetl))infun()->Lazy.forcenextexceptionForced_twiceletreconceseq=letconsumed=reffalseinfun()->if!consumedthenraiseForced_twice;consumed:=true;matchseq()with|Nil->Nil|Cons(hd,tl)->Cons(hd,oncetl)letrectransposeseq()=matchseq()with|Nil->Nil|Cons(hd,tl)->letfirst()=lethd_optseq=matchseq()with|Nil->None|Cons(hd,_tl)->Somehdinlettl'=filter_maphd_opttlinmatchhd()with|Nil->tl'()|Cons(hd,_tl)->Cons(hd,tl')inletothers()=lettl_optseq=matchseq()with|Nil->None|Cons(_hd,tl)->Sometlinlettl'=filter_maptl_opttlinmatchhd()with|Nil->tl'()|Cons(_hd,tl)->Cons(tl,tl')inifis_emptyfirstthenNilelseCons(first,transposeothers)letreczipab()=matcha()with|Nil->Nil|Cons(a_hd,a_tl)->matchb()with|Nil->Nil|Cons(b_hd,b_tl)->Cons((a_hd,b_hd),zipa_tlb_tl)letrecmap2fab()=matcha()with|Nil->Nil|Cons(a_hd,a_tl)->matchb()with|Nil->Nil|Cons(b_hd,b_tl)->Cons(fa_hdb_hd,map2fa_tlb_tl)letrecinterleaveab()=matcha()with|Nil->b()|Cons(hd,tl)->Cons(hd,interleavebtl)letrecsorted_merge1loa_cella_hda_tlb()=matchb()with|Nil->a_cell|Cons(b_hd,b_tl)asb_cell->sorted_merge1oa_cella_hda_tlb_cellb_hdb_tlandsorted_merge1roab_cellb_hdb_tl()=matcha()with|Nil->b_cell|Cons(a_hd,a_tl)asa_cell->sorted_merge1oa_cella_hda_tlb_cellb_hdb_tlandsorted_merge1oa_cella_hda_tlb_cellb_hdb_tl=ifoa_hdb_hd<=0thenCons(a_hd,sorted_merge1roa_tlb_cellb_hdb_tl)elseCons(b_hd,sorted_merge1loa_cella_hda_tlb_tl)letsorted_mergeoab()=matcha(),b()with|Nil,Nil->Nil|Nil,c|c,Nil->c|Cons(a_hd,a_tl)asa_cell,(Cons(b_hd,b_tl)asb_cell)->sorted_merge1oa_cella_hda_tlb_cellb_hdb_tlletrecmap_product1fa_hda_tlb=matchb()with|Nil->Nil|Cons(b_hd,b_tl)->Cons(fa_hdb_hd,append(map(funai->faib_hd)a_tl)(fun()->map_product1fa_hda_tlb_tl))letmap_productfab()=matcha()with|Nil->Nil|Cons(a_hd,a_tl)->map_product1fa_hda_tlbletproductab=map_product(funab->(a,b))abletunzipseq=(mapfstseq,mapsndseq)letsplit=unzipletpartition_mapfseq=filter_map(funx->Stdcompat__either.find_left(fx))seq,filter_map(funx->Stdcompat__either.find_right(fx))seqletpartitionpseq=filterpseq,filter(funx->not(px))seqletrecof_dispenserf()=matchf()with|None->Nil|Someitem->Cons(item,of_dispenserf)letto_dispenserseq=letseq_ref=refseqinfun()->match!seq_ref()with|Nil->None|Cons(hd,tl)->seq_ref:=tl;Somehdletrecintsi()=Cons(i,ints(succi))