Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (* LISTA 1 *)
- let rec flatten1 lsts =
- if lsts = [] then []
- else (List.hd lsts)@flatten1(List.tl lsts);;
- flatten1 [[1;2;4];[6;2];[2;3]];;
- let rec count (i,lst) =
- if lst=[] then 0
- else if List.hd lst = i then 1+count(i,List.tl lst)
- else count(i,List.tl lst);;
- count (3,[1;2;3;4;5;4;3;2;3;8;3;3]);;
- count (3,[]);;
- let rec duplicate (ob,i) =
- if i=0 then []
- else ob::duplicate(ob,i-1);;
- duplicate (4,4);;
- let rec sqrList lst =
- if lst = [] then []
- else ((List.hd lst) * (List.hd lst))::sqrList(List.tl lst);;
- sqrList [1;2;3;7];;
- let palindrome lst =
- List.rev lst = lst;;
- palindrome [1;2;3;4;5;4;3;2;1];;
- palindrome [4;6;9;5;4];;
- let rec listLength lst =
- if lst=[] then 0
- else 1+listLength(List.tl lst);;
- listLength [1;2;3;4;5;6;3;4];;
- let rec listAppend (l1,l2) =
- if l1=[] then l2
- else List.hd l1::listAppend(List.tl l1,l2);;
- listAppend ([1;3;5;4;],[2;3;2;3;11]);;
- (* LISTA 2 *)
- let rec fib1 i =
- if i=0 then 0
- else if i=1 then 1
- else fib1(i-2)+fib1(i-1);;
- let rec fib2 i =
- if i=0 then 0
- else if i=1 then 1
- else
- let rec fib3 (i,acc,acc2) =
- if i=0 then acc
- else fib3 (i-1,acc+acc2, acc)
- in fib3 (i-2,1,1);;
- (*# fib1 37;;
- # fib2 37;;*)
- let root3 a =
- let rec help(acc) =
- if abs_float(acc**3. -. a) <= 1E-15 *. abs_float(a) then acc
- else help(acc+.((a/.(acc**2.) -. acc)/.3.))
- in if a >= 1. then help(a/.3.) else help(a);;
- root3 27.;;
- let (_,_,p1,_,_) = (-2,-1,0,1,2);;
- let ( _ ,(p3,_)) = ((1,2),(0,1));;
- let rec initsegmet (l1,l2) =
- if l1=[] then true
- else if List.hd l1 = List.hd l2 then initsegment (List.tl l1,List.tl l2)
- else false;;
- initsegment ([1;2],[1;3;5;7;3;2;1]);;
- let rec replace_nth (lst,n,i) =
- if n=0 then i::(List.tl lst)
- else if n>=List.length lst then raise (Failure "za krotka lista")
- else List.hd lst::replace_nth(List.tl lst,n-1,i);;
- replace_nth ([1;2;3;4],3,9);;
Add Comment
Please, Sign In to add comment