Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type 'a stream=Cons of 'a*(unit->'a stream);;
- type history={reference:int list;iter:int};;
- let le=[1;2;4;6;8;10];;
- let rec from_list l r=match l with
- |[]->(Cons(r,(fun ()->(from_list [] r))))
- |x::xs->(Cons(x,(fun ()->(from_list xs r))));;
- let se=from_list le 5;;
- let rec to_list n (Cons(v,f))=match n with
- |0->[]
- |_->[v]@(to_list (n-1) (f ()));;
- let rec element_at n (Cons(v,f))=match n with
- |0->v
- |_->element_at (n-1) (f ());;
- let head (Cons(v,f))=v;;
- let tail (Cons(v,f))=f ();;
- let rec map p (Cons(v,f))=(Cons((p v),(fun ()->map p (f ()))));;
- let rec from_k k=(Cons(k,(fun ()->from_k (k+1))));;
- let nat=from_k 0;;
- let fib=
- let rec temp x y=(Cons(x,(fun ()->temp y (x+y)))) in
- temp 0 1;;
- let rec zip f (Cons(v1,g1)) (Cons(v2,g2))=(Cons((f v1 v2),(fun ()->zip f (g1 ()) (g2 ()))));;
- let multiples_of n=
- let rec temp st z=(Cons(z*st,(fun ()->temp st (z+1)))) in
- temp n 0;;
- let multiples=
- let rec temp n=(Cons((multiples_of n),(fun ()->temp (n+1)))) in
- temp 0;;
- let rec stream_at n (Cons(s,f))=match n with
- |1->s
- |_->stream_at (n-1) (f ())
- let rec element_at_l n l=match n with
- |1->List.hd l
- |_->element_at_l (n-1) (List.tl l);;
- let rec exists v l=match l with
- |[]->false
- |x::xs->if v==x then true else exists v (List.tl l);;
- let unique l=
- let rec temp uniq orig=match orig with
- |[]->uniq
- |x::xs->if exists x uniq then temp uniq xs else temp (uniq@[x]) xs in
- temp [] l;;
- let rec list_of_streams n (Cons(s,f))=match n with
- |1->(to_list 1 s)
- |_->(to_list n s)@(list_of_streams (n-1) (f ()));;
- let cut_overlap src refer=
- let rec temp res s r=match s with
- |[]->res
- |x::xs->if exists x r
- then temp res (List.tl s) r
- else temp (res@[(List.hd s)]) (List.tl s) r in
- temp [] src refer;;
- let rec next_uniq hist (Cons(s,f))=
- let curr=cut_overlap (unique (list_of_streams hist.iter (Cons(s,f)))) hist.reference in
- match (List.length curr) with
- |0->next_uniq {reference=hist.reference;iter=(hist.iter+1)} (Cons(s,f))
- |_->{reference=(List.hd curr)::hist.reference;iter=hist.iter};;
- let flatten (Cons(s,f))=
- let rec temp hist=
- let h=next_uniq hist (Cons(s,f)) in
- (Cons((List.hd h.reference),(fun ()->temp h))) in
- temp {reference=[];iter=1};;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement