Advertisement
Guest User

Untitled

a guest
Nov 14th, 2016
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. type 'a llist = LNil | LCons of 'a * (unit -> 'a llist);;
  2.  
  3. let rec ltake = function
  4. (0, _) -> []
  5. | (_, LNil) -> []
  6. | (n, LCons(x,xf)) -> x::ltake(n-1, xf())
  7. ;;
  8. # val ltake : int * 'a llist -> 'a list = <fun>
  9.  
  10.  
  11. let rec toLazyList = function
  12. [] -> LNil
  13. | h::t -> LCons(h, function () -> toLazyList t);;
  14. val toLazyList : 'a list -> 'a llist = <fun>
  15.  
  16.  
  17. let rec replicateElements (k, llist) =
  18. let rec replicate = function
  19. | (_, LNil) -> LNil
  20. | (n, LCons(h, xf)) ->
  21. if n = 0 then replicate(k, xf())
  22. else LCons(h, fun () -> replicate (n - 1, xf()))
  23. in replicate(k, llist)
  24.  
  25.  
  26. (*TEST*)
  27. ltake(10,replicateElements(3,toLazyList[1,2,3,4]))
  28. ;;
  29. - : (int * int * int * int) list = [(1, 2, 3, 4)](*Powinno wyświetlić 10 elementów listy czyli[(1,1,1,2,2,2,3,3,3,4)]*)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement