Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. type 'a llist = LNil | LCons of 'a * (unit -> 'a llist);;
  2.  
  3. let rec lfrom k = LCons (k, function () -> lfrom (k+1));;
  4.  
  5. let rec ltake = function
  6. (0, _) -> []
  7. | (_, LNil) -> []
  8. | (n, LCons(x,xf)) -> x::ltake(n-1, xf())
  9. ;;
  10. ltake (4,lfrom 7);; (* - : int list = [7; 8; 9; 10] *)
  11.  
  12. (* 1 *)
  13. let rec lpolacz(ll1, ll2) =
  14. match ll1 with
  15. LCons(x, xt) -> LCons(x, fun()-> lpolacz(ll2, xt()))
  16. | LNil when ll2<>LNil -> lpolacz(ll2, ll1)
  17. | _ -> LNil
  18.  
  19. ltake(10, lpolacz(lfrom(1), lfrom(11)));;
  20. ltake(22, lpolacz(lfrom(-10), lfrom(0)));;
  21. ltake(10, lpolacz(lfrom(0), LNil));;
  22. ltake(10, lpolacz(LNil, lfrom(-10)));;
  23. ltake(10, lpolacz(LNil, LNil));;
  24.  
  25. let rec polacz(l1, l2) =
  26. match l1 with
  27. h::t -> h::polacz(l2,t)
  28. | [] when l2<>[] -> polacz(l2,l1)
  29. | _ -> []
  30.  
  31. polacz([1;3;5;7;9;11], [2;4;6;8;10])
  32. polacz([2;2], [1;1;1;1])
  33. polacz([], [])
  34. polacz([1;2;3], [])
  35. polacz([], [4;5;6])
  36.  
  37. (* 2 *)
  38. let mulListToLazy l x =
  39. let rec mul = function
  40. [] -> LNil
  41. | h::t -> LCons(h*x, function()-> mul t)
  42. in mul l;;
  43.  
  44. mulListToLazy [2;3;4;2;3;4] 10;;
  45. mulListToLazy [66; 2; 2332] 2;;
  46. ltake(5, (mulListToLazy [2;3;4;5;6;7;8;9;10] 10));;
  47. ltake(3, mulListToLazy [1;2;3;4;5] (-1));;
  48.  
  49.  
  50. //3
  51. def cGeo(a:Int, iloraz:Int)=
  52. {
  53. def geoH(a1:Int, q:Int):Stream[Int] =
  54. {
  55. println(a1)
  56. a1#::geoH(a1*q, q)
  57. }
  58. geoH(a, iloraz)
  59. }
  60.  
  61. cGeo(2, 2)
  62. cGeo(10, (-1)).take(5)
  63. cGeo(10, (-1)).take(5) toList
  64. cGeo(2, 2).take(7) toList
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement