Guest User

Untitled

a guest
Jan 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.95 KB | None | 0 0
  1. (* LISTA 1 *)
  2.  
  3. let rec flatten1 lsts =
  4.   if lsts = [] then []
  5.   else (List.hd lsts)@flatten1(List.tl lsts);;
  6.  
  7. flatten1 [[1;2;4];[6;2];[2;3]];;
  8.  
  9. let rec count (i,lst) =
  10.   if lst=[] then 0
  11.   else if List.hd lst = i then 1+count(i,List.tl lst)
  12.   else count(i,List.tl lst);;
  13.  
  14. count (3,[1;2;3;4;5;4;3;2;3;8;3;3]);;
  15. count (3,[]);;
  16.  
  17. let rec duplicate (ob,i) =
  18.   if i=0 then []
  19.   else  ob::duplicate(ob,i-1);;
  20.  
  21. duplicate (4,4);;
  22.  
  23. let rec sqrList lst =
  24.   if lst = [] then []
  25.   else ((List.hd lst) * (List.hd lst))::sqrList(List.tl lst);;
  26.  
  27. sqrList [1;2;3;7];;
  28.  
  29. let palindrome lst =
  30.   List.rev lst = lst;;
  31.  
  32. palindrome [1;2;3;4;5;4;3;2;1];;
  33. palindrome [4;6;9;5;4];;
  34.  
  35. let rec listLength lst =
  36.   if lst=[] then 0
  37.   else 1+listLength(List.tl lst);;
  38.  
  39. listLength [1;2;3;4;5;6;3;4];;
  40.  
  41. let rec listAppend (l1,l2) =
  42.   if l1=[] then l2
  43.   else List.hd l1::listAppend(List.tl l1,l2);;
  44.  
  45. listAppend ([1;3;5;4;],[2;3;2;3;11]);;
  46.  
  47.  
  48. (* LISTA 2 *)
  49.  
  50.  
  51. let rec fib1 i =
  52.   if i=0 then 0
  53.   else if i=1 then 1
  54.   else fib1(i-2)+fib1(i-1);;
  55.  
  56. let rec fib2 i =
  57.   if i=0 then 0
  58.   else if i=1 then 1
  59.   else
  60.     let rec fib3 (i,acc,acc2) =
  61.       if i=0 then acc
  62.       else fib3 (i-1,acc+acc2, acc)
  63.     in fib3 (i-2,1,1);;
  64.  
  65. (*# fib1 37;;
  66. # fib2 37;;*)
  67.  
  68. let root3 a =
  69.   let rec help(acc) =
  70.     if abs_float(acc**3. -. a) <= 1E-15 *. abs_float(a) then acc
  71.     else help(acc+.((a/.(acc**2.) -. acc)/.3.))
  72.   in if a >= 1. then help(a/.3.) else help(a);;
  73.  
  74. root3 27.;;
  75.  
  76. let (_,_,p1,_,_) = (-2,-1,0,1,2);;
  77. let ( _ ,(p3,_)) = ((1,2),(0,1));;
  78.  
  79.  
  80. let rec initsegmet (l1,l2) =
  81.   if l1=[] then true
  82.   else if List.hd l1 = List.hd l2 then initsegment (List.tl l1,List.tl l2)
  83.   else false;;
  84.  
  85. initsegment ([1;2],[1;3;5;7;3;2;1]);;
  86.  
  87. let rec replace_nth (lst,n,i) =
  88.   if n=0 then i::(List.tl lst)
  89.   else if n>=List.length lst then raise (Failure "za krotka lista")
  90.   else List.hd lst::replace_nth(List.tl lst,n-1,i);;
  91.  
  92. replace_nth ([1;2;3;4],3,9);;
Add Comment
Please, Sign In to add comment