Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type 'a llist = LNil | LCons of 'a * (unit -> 'a llist);;
- let rec ltake = function
- (0, _) -> []
- | (_, LNil) -> []
- | (n, LCons(x,xf)) -> x::ltake(n-1, xf())
- ;;
- # val ltake : int * 'a llist -> 'a list = <fun>
- let rec toLazyList = function
- [] -> LNil
- | h::t -> LCons(h, function () -> toLazyList t);;
- val toLazyList : 'a list -> 'a llist = <fun>
- let rec replicateElements (k, llist) =
- let rec replicate = function
- | (_, LNil) -> LNil
- | (n, LCons(h, xf)) ->
- if n = 0 then replicate(k, xf())
- else LCons(h, fun () -> replicate (n - 1, xf()))
- in replicate(k, llist)
- (*TEST*)
- ltake(10,replicateElements(3,toLazyList[1,2,3,4]))
- ;;
- - : (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