Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. type 'a stream=Cons of 'a*(unit->'a stream);;
  2. type history={reference:int list;iter:int};;
  3. let le=[1;2;4;6;8;10];;
  4. let rec from_list l r=match l with
  5.     |[]->(Cons(r,(fun ()->(from_list [] r))))
  6.     |x::xs->(Cons(x,(fun ()->(from_list xs r))));;
  7. let se=from_list le 5;;
  8. let rec to_list n (Cons(v,f))=match n with
  9.     |0->[]
  10.     |_->[v]@(to_list (n-1) (f ()));;
  11. let rec element_at n (Cons(v,f))=match n with
  12.     |0->v
  13.     |_->element_at (n-1) (f ());;
  14. let head (Cons(v,f))=v;;
  15. let tail (Cons(v,f))=f ();;
  16. let rec map p (Cons(v,f))=(Cons((p v),(fun ()->map p (f ()))));;
  17. let rec from_k k=(Cons(k,(fun ()->from_k (k+1))));;
  18. let nat=from_k 0;;
  19. let fib=
  20.     let rec temp x y=(Cons(x,(fun ()->temp y (x+y)))) in
  21.     temp 0 1;;
  22. let rec zip f (Cons(v1,g1)) (Cons(v2,g2))=(Cons((f v1 v2),(fun ()->zip f (g1 ()) (g2 ()))));;
  23. let multiples_of n=
  24.     let rec temp st z=(Cons(z*st,(fun ()->temp st (z+1)))) in
  25.     temp n 0;;
  26. let multiples=
  27.     let rec temp n=(Cons((multiples_of n),(fun ()->temp (n+1)))) in
  28.     temp 0;;
  29. let rec stream_at n (Cons(s,f))=match n with
  30.     |1->s
  31.     |_->stream_at (n-1) (f ())
  32. let rec element_at_l n l=match n with
  33.     |1->List.hd l
  34.     |_->element_at_l (n-1) (List.tl l);;
  35. let rec exists v l=match l with
  36.     |[]->false
  37.     |x::xs->if v==x then true else exists v (List.tl l);;
  38. let unique l=
  39.     let rec temp uniq orig=match orig with
  40.     |[]->uniq
  41.     |x::xs->if exists x uniq then temp uniq xs else temp (uniq@[x]) xs in
  42.     temp [] l;;
  43. let rec list_of_streams n (Cons(s,f))=match n with
  44.     |1->(to_list 1 s)
  45.     |_->(to_list n s)@(list_of_streams (n-1) (f ()));;
  46. let cut_overlap src refer=
  47.     let rec temp res s r=match s with
  48.         |[]->res
  49.         |x::xs->if exists x r
  50.         then temp res (List.tl s) r
  51.         else temp (res@[(List.hd s)]) (List.tl s) r in
  52.     temp [] src refer;;
  53. let rec next_uniq hist (Cons(s,f))=
  54.     let curr=cut_overlap (unique (list_of_streams hist.iter (Cons(s,f)))) hist.reference in
  55.     match (List.length curr) with
  56.         |0->next_uniq {reference=hist.reference;iter=(hist.iter+1)} (Cons(s,f))
  57.         |_->{reference=(List.hd curr)::hist.reference;iter=hist.iter};;
  58. let flatten (Cons(s,f))=
  59.     let rec temp hist=
  60.         let h=next_uniq hist (Cons(s,f)) in
  61.             (Cons((List.hd h.reference),(fun ()->temp h))) in
  62.     temp {reference=[];iter=1};;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement