Pabl0o0

Untitled

Nov 6th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. (*zad1*)
  2. let filtering list_lists value =
  3. match list_lists with
  4. |[]->[]
  5. |_ -> List.filter(fun x-> List.filter(fun y -> y=value)x <> []) list_lists;;
  6.  
  7. filtering [[1;2;3];[3;4];[5;6]] 3;;
  8. filtering [[1;2;4;5;6;7];[1;1;1;1;4];[5;6];[5;6;7;5;76;7;54];[5;6;5;4;3];[5;6]] 3;;
  9. filtering [[1;2;4;5;6;7];[1;1;1;1;4];[5;6];[5;6;7;5;76;7;54];[5;6;5;4];[5;6]] 3;;
  10. filtering [] 5;;
  11.  
  12.  
  13. (*zad2*)
  14. let binary list =
  15. List.fold_left (fun acc x -> acc*2 + x) 0 list;;
  16.  
  17. binary [1;1;1;0];;
  18. binary [1;1;1;0;1];;
  19. binary [];;
  20. binary [0;0;0;0;0;0;1];;
  21.  
  22.  
  23.  
  24. (*inaczej*)
  25. let bin2dec lb =
  26. List.fold_left (fun acc b -> acc*2 + b) 0 lb;;
  27.  
  28. bin2dec [1;1;1;0;1];;
  29.  
  30. let binarny list=
  31. let rec binarny1(lista, wynik, index) =
  32. match lista with
  33. | [] -> wynik
  34. | h::t ->
  35. if h = 0 then binarny1(t, wynik, index+.1.)
  36. else let p = 2.**index in binarny1(t, wynik+.p, index+.1.)
  37. in binarny1(List.rev list,0.,0.);;
  38.  
  39. binarny [1;1;1;0;1];;
  40.  
  41. (*zad3*)
  42. (*not tail-recursive*)
  43. let modulus tuple=
  44. List.map (fun (x,y,z)-> (abs(x),abs(y),abs(z))) tuple;;
  45.  
  46. (*tail-recursive*)
  47. let modulus tuple=
  48. let rec modulus_2 (tuple2, list) =
  49. match tuple2 with
  50. | [] -> List.rev list
  51. | (a,b,c)::t -> modulus_2(t,(abs(a),abs(b),abs(c))::list)
  52. in modulus_2 (tuple, []);;
  53.  
  54.  
  55. modulus [(-1,-2,-3);(-1,2,4)];;
  56. modulus [(-1,-2,-3);(-1,2,4);(0,0,-10)];;
  57. modulus [];;
  58.  
  59.  
  60. let binary list =
  61. List.fold_left (fun acc x -> acc*2 + x) 0 list;;
  62.  
  63. let rec jakasFunkcja2 x y = List.rev(List.fold_left (fun acc h1 -> h1::a) [] y x);;
  64.  
  65. let rec jakasFunkcja2 x y = List.rev(List.fold_left (fun a (x as h1::t1 -> (h2,h1)::a) [] y x);;
  66. jakasFunkcja2 [1;2;3] [4;5;6];;
Advertisement
Add Comment
Please, Sign In to add comment