Advertisement
Guest User

file8.ml

a guest
Oct 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.95 KB | None | 0 0
  1. let combine1 l =
  2.   let rec aux acc lst = match lst with
  3.     | [] -> acc
  4.     | h::t -> aux (h@acc) t
  5.   in aux [] (List.rev l)
  6.  
  7. let combine2 l = List.fold_right (fun el acc -> el@acc) l []        
  8. let combine3 l = List.fold_left (fun acc el -> el@acc) [] l
  9.                                  
  10. let mylst = [[1;2]; [3;4;5]; []; [6]]
  11.  
  12. let sumPair1 l =
  13.   let rec aux acc1 acc2 lst = match lst with
  14.     | [] -> (acc1, acc2)
  15.     | h::t -> match h with
  16.               | (a, b) -> aux (acc1+a) (acc2+b) t
  17.   in aux 0 0 l
  18.  
  19. let mylst2 = [(1, 2); (4, 5); (5, 7); (9, 11)]
  20.  
  21. let sumPair2 l = List.fold_right (fun (a, b) (x, y) -> (x+a, y+b)) l (0, 0)
  22.  
  23. let nrEl l = List.fold_right (fun el acc -> acc+1) l 0
  24.  
  25. let maxLst l = List.fold_right (fun el acc -> if el > acc then el else acc) l 0
  26.  
  27. let lstComp l1 l2 =
  28.   let rec aux lst1 lst2 = match lst1, lst2 with
  29.     | [], [] -> 0
  30.     | [], a -> -1
  31.     | a, [] -> 1
  32.     | h1::t1, h2::t2 -> if h1 = h2 then aux t1 t2 else
  33.                           if h1 > h2 then 1 else -1
  34.   in aux l1 l2
  35.  
  36. let lsta = [1; 2; 3; 4]
  37.  
  38. let fromTo a b =
  39.   let rec aux acc n =
  40.     if n = a then n::acc
  41.     else aux (n::acc) (n-1)
  42.   in aux [] b
  43.  
  44. let xlst = fromTo 1 100
  45.          
  46. let prod =  List.fold_right
  47.               (fun el acc -> if (el mod 2) != 0 then el * acc else acc) xlst 1
  48.  
  49. let cuburi = List.fold_right
  50.                (fun el acc -> if (el mod 5) = 0 then (el*el*el)+acc else acc) xlst 0
  51.  
  52. module Int = struct
  53.   type t = int
  54.   let compare = compare
  55. end
  56. module IS = Set.Make(Int)
  57.  
  58. let seta = IS.add 3 (IS.add 7 (IS.add 5 (IS.singleton(1))))
  59.  
  60. let setb = IS.add 2 (IS.add 1 (IS.add 6 (IS.add 4 (IS.singleton(3)))))
  61.  
  62. let myinter set1 set2 = IS.fold (fun el acc -> if IS.mem el set2 then IS.add el acc else acc) set1 IS.empty
  63.  
  64. let myreun set1 set2 = IS.fold (fun el acc -> IS.add el acc) set1 set2
  65.  
  66. let mydiff set1 set2 = IS.fold (fun el acc -> if not (IS.mem el set2) then IS.add el acc else acc) set1 IS.empty
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement