Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.10 KB | None | 0 0
  1.  
  2. (* Zadanie 2a*)
  3. let rec fib n =
  4.     match n<2 with
  5.         | true -> if n < 0 then failwith "ujemny argument" else n
  6.         | false -> fib(n-2) + fib(n-1)
  7.  
  8. (* Zadanie 2b*)
  9.  
  10. let fib_tl n =
  11.     let rec fib_tl_h n ak w =
  12.     match n = 0 with
  13.         | true ->  ak
  14.         | false -> fib_tl_h (n-1) w (ak+w)
  15.     in
  16.     match n>0 with
  17.         | true -> fib_tl_h n 0 1
  18.         | false -> failwith "ujemny argument"
  19.    
  20. (* Zadanie 3*)
  21. let root3 a =
  22.     let rec root3_rt x =
  23.         match abs_float(x ** 3. -. a) <= 1e-15 *. abs_float(a) with
  24.         | true -> x
  25.         | false -> root3_rt (x +. ( a /. (x ** 2.) -. x) /. 3.)
  26.    
  27.     in match a <= 1. with
  28.         | true -> root3_rt(a)
  29.         | false -> root3_rt(a/.3.)
  30.        
  31.    
  32.    
  33. (* Zadanie 4a*)
  34. let [_;_;x;_;_] = [-2;-1;0;1;2]
  35.  
  36. (* Zadanie 4b*)
  37. let [(_,_);(y,_)] = [(1,2);(0,1)]
  38.  
  39. (* Zadanie 5*)
  40. let rec initSegment (lista1, lista2) =
  41.     match (lista1, lista2) with
  42.     | ([],_) -> true
  43.     | (h1::t1,h2::t2) ->  h1=h2 && initSegment(t1,t2)
  44.     | _ -> false
  45.    
  46. (* Zadanie 6*) 
  47. let rec replaceNth(lista, nth, elem) =
  48.     match lista with
  49.     |   [] -> []
  50.     |   h::t -> (if nth=0 then elem else h) :: replaceNth (t,(nth-1), elem)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement