Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (* module de multimi *)
- module Int = struct
- type t = int
- let compare = compare
- end
- module IS = Set.Make(Int)
- module CS = Set.Make(Char)
- module CSS = Set.Make(CS)
- (* multimi *)
- let charset = CS.add 'a' (CS.add 'b' (CS.singleton 'c'))
- let coolset = IS.add 112 (IS.add 67 (IS.add 32 (IS.add 270 (IS.singleton 955))))
- (* afiseaza elementele multimilor continute intr-o multime ca o lista de liste *)
- let psos set = CSS.elements set |> List.map CS.elements
- (* exercitiul 5 *)
- let digset =
- let rec aux acc = function
- | 0 -> acc
- | n -> aux (IS.add (n mod 10) acc) (n / 10)
- in aux IS.empty
- let digset2 set = IS.fold (fun elt s -> IS.union (digset elt) s) set IS.empty
- let digset3 set = IS.fold (fun elt s -> IS.inter (digset elt) s) set (digset (IS.choose set))
- (* exercitiul 6 *)
- let subsets set =
- let rec aux acc set = if CS.is_empty set = true then acc else
- aux ( CSS.union acc (CSS.fold (fun elt sacc -> CSS.add ( CS.union elt (CS.singleton (CS.choose set)) ) sacc) acc CSS.empty) ) (CS.diff set (CS.singleton (CS.choose set)))
- in aux (CSS.add CS.empty CSS.empty) set
- let css = subsets charset
- (* exercitiul 8 *)
- let ksubsets set card =
- let allssets = subsets set in
- CSS.filter (fun elt -> CS.cardinal elt = card) allssets
- (* exercitiul 9 *)
- let calc a b lim =
- let rec aux acc (a1, b1) (a2, b2) =
- if a1 + b1 < lim || a2 + b2 < lim then
- if a1 + b1 < lim then aux (IS.add (a1+b1) (IS.add b1 acc)) ((a1+b1), b1) (a2, b2)
- else aux (IS.add (a2+b2) (IS.add b2 acc)) (a1, b1) ((a2+b2), b2)
- else IS.elements acc
- in aux IS.empty (a, b) (b, a)
- (* Tema 5 dupa curs, Ex. 2 *)
- let explode s =
- let rec expl i l =
- if i < 0 then l else
- expl (i - 1) (s.[i] :: l) in
- expl (String.length s - 1) [];;
- module S = Map.Make(Char);;
- let lsttomap lst =
- let rec aux acc lst = match lst with
- | [] -> acc
- | h :: t -> if S.mem h acc then aux (S.add h ((S.find h acc) + 1) acc) t else aux (S.add h 1 acc) t
- in aux S.empty lst
- let explode s =
- let rec expl i l =
- if i < 0 then l else
- expl (i - 1) (s.[i] :: l) in
- expl (String.length s - 1) [];;
- module S = Map.Make(Char);;
- let lsttomap lst =
- let rec aux acc lst = match lst with
- | [] -> acc
- | h :: t -> if S.mem h acc then aux (S.add h ((S.find h acc) + 1) acc) t else aux (S.add h 1 acc) t
- in aux S.empty lst
- let lsttomap2 lst =
- let rec aux acc lst = match lst with
- | [] -> acc
- | h :: t -> aux (S.add h 1 acc) t
- in aux S.empty lst
- (* FAILED TRY
- let digset3 set =
- let rec aux acc set = match set with
- | IS.empty -> acc
- | IS.elements -> aux (IS.inter (digset (IS.choose set)) (digset IS.iter ) (IS.remove (IS.choose set) set)
- in aux IS.empty set
- *)
- (* exercitiul 6 *)
- let subsets set =
- let rec aux parcurse acc set = if CS.is_empty set = true then acc else
- aux (CS.add (CS.choose set) parcurse) (CSS.fold (fun elt sacc -> (CSS.union (CSS.singleton elt) (CSS.singleton (CS.singleton (CS.choose set))) )) acc CSS.empty) (CS.diff set (CS.singleton (CS.choose set)))
- in aux CS.empty (CSS.add CS.empty CSS.empty) set
- let css = subsets charset
- let subsets set =
- let rec aux acc set = if CS.is_empty set = true then acc else
- aux (CSS.union acc (CSS.fold (fun elt sacc -> CSS.add ((CS.singleton (CS.choose set)) (CSS.singleton elt)) sacc) acc CSS.empty))(CS.diff set (CS.singleton (CS.choose set)))
- in aux (CSS.add CS.empty CSS.empty) set
- let css = subsets charset
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement