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 lfrom k = LCons (k, function () -> lfrom (k+1));;
- let rec ltake = function
- (0, _) -> []
- | (_, LNil) -> []
- | (n, LCons(x,xf)) -> x::ltake(n-1, xf())
- ;;
- ltake (4,lfrom 7);; (* - : int list = [7; 8; 9; 10] *)
- (* 1 *)
- let rec lpolacz(ll1, ll2) =
- match ll1 with
- LCons(x, xt) -> LCons(x, fun()-> lpolacz(ll2, xt()))
- | LNil when ll2<>LNil -> lpolacz(ll2, ll1)
- | _ -> LNil
- ltake(10, lpolacz(lfrom(1), lfrom(11)));;
- ltake(22, lpolacz(lfrom(-10), lfrom(0)));;
- ltake(10, lpolacz(lfrom(0), LNil));;
- ltake(10, lpolacz(LNil, lfrom(-10)));;
- ltake(10, lpolacz(LNil, LNil));;
- let rec polacz(l1, l2) =
- match l1 with
- h::t -> h::polacz(l2,t)
- | [] when l2<>[] -> polacz(l2,l1)
- | _ -> []
- polacz([1;3;5;7;9;11], [2;4;6;8;10])
- polacz([2;2], [1;1;1;1])
- polacz([], [])
- polacz([1;2;3], [])
- polacz([], [4;5;6])
- (* 2 *)
- let mulListToLazy l x =
- let rec mul = function
- [] -> LNil
- | h::t -> LCons(h*x, function()-> mul t)
- in mul l;;
- mulListToLazy [2;3;4;2;3;4] 10;;
- mulListToLazy [66; 2; 2332] 2;;
- ltake(5, (mulListToLazy [2;3;4;5;6;7;8;9;10] 10));;
- ltake(3, mulListToLazy [1;2;3;4;5] (-1));;
- //3
- def cGeo(a:Int, iloraz:Int)=
- {
- def geoH(a1:Int, q:Int):Stream[Int] =
- {
- println(a1)
- a1#::geoH(a1*q, q)
- }
- geoH(a, iloraz)
- }
- cGeo(2, 2)
- cGeo(10, (-1)).take(5)
- cGeo(10, (-1)).take(5) toList
- cGeo(2, 2).take(7) toList
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement