Pabl0o0

Untitled

Nov 14th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. type 'a nlist = Koniec | Element of 'a * ('a nlist);;
  2.  
  3. let rec podziel nlista =
  4. match nlista with
  5. | Element(h,t) -> let (lista1, lista2) = podziel t in if h mod 2 <> 0 then (dolacz lista1 h, lista2) else (lista1, dolacz lista2 h)
  6. | Koniec -> (Koniec,Koniec);;
  7.  
  8. let rec dolacz nlista x =
  9. Element(x,nlista);;
  10.  
  11. let nlist = Element(5, Element(6,Element(3, Element(2, Element(1,Koniec)))));;
  12. podziel nlist;;
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19. type 'a llist = LKoniec | LElement of 'a * (unit -> 'a llist);;
  20.  
  21. let ltl = function
  22. |LKoniec -> failwith "ltl"
  23. | LElement (_, xf) -> xf();;
  24.  
  25. let rec ltake = function
  26. |(0, _) -> []
  27. | (_, LKoniec) -> []
  28. | (n, LElement(x,xf)) -> x::ltake(n-1, xf());;
  29.  
  30. let rec toLazyList = function
  31. |[] -> LKoniec
  32. | h::t -> LElement(h, function () -> toLazyList t);;
  33.  
  34. let rec (@$) ll1 ll2 =
  35. match ll1 with
  36. |LKoniec -> ll2
  37. | LElement(x, xf) -> LElement(x, function () -> (xf()) @$ ll2);;
  38.  
  39. let rec dolacz llista x =
  40. let ll1 = LElement(x,function ()->LKoniec) in ll1 @$ llista ;;
  41.  
  42. let rec lpodziel llista =
  43. match llista with
  44. | LElement (x,xf) ->let (lista1, lista2) = lpodziel (ltl llista) in if x mod 2 <> 0 then (dolacz lista1 x, lista2) else (lista1, dolacz lista2 x)
  45. | LKoniec -> (LKoniec, LKoniec);;
  46.  
  47.  
  48. let l1 =toLazyList[5;6;3;2;1];;
  49. let l2 = lpodziel l1;;
  50.  
  51. let convert llisty=
  52. match llisty with
  53. | (LKoniec, LKoniec) ->([],[])
  54. | (LElement(x,xf),LKoniec) -> (x::ltake(5,xf()),[])
  55. | (LKoniec, LElement(x,xf)) -> ([],x::ltake(5,xf()))
  56. | (LElement(x1,xf1),LElement(x2,xf2))-> (x1::ltake(5,xf1()),x2::ltake(5,xf2()));;
  57.  
  58. convert l2;;
Advertisement
Add Comment
Please, Sign In to add comment