Last active
August 29, 2015 14:03
-
-
Save hsk/7b6a8758474bb5b3ecbd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Format | |
let pp_ls f ppf ls = | |
let rec loop ppf = function | |
| [] -> () | |
| [x] -> fprintf ppf "%a@?" f x | |
| x::xs -> fprintf ppf "%a, %a" f x loop xs | |
in | |
fprintf ppf "{%a}@?" loop ls | |
let pp_i ppf i = fprintf ppf "%d@?" i | |
module N = struct | |
module IntSet = Set.Make( | |
struct | |
let compare = Pervasives.compare | |
type t = int | |
end ) | |
include IntSet | |
let of_list l = List.fold_left (fun s e -> add e s) empty l | |
let pp ppf s = pp_ls pp_i ppf (elements s) | |
let powerset s = | |
let rec ps = function | |
| [] -> [[]] | |
| h::t -> List.fold_right (fun t xs -> (h::t)::t::xs) (ps t) [] | |
in ps(elements s) | |
let pp_l ppf ls = pp_ls pp_i ppf ls | |
let pp_ss ppf ss = pp_ls pp_l ppf ss | |
end | |
let a = N.of_list [1;2;3] | |
let b = N.of_list [3;4;5;6] | |
let _ = | |
fprintf std_formatter "a = %a@." N.pp a; | |
fprintf std_formatter "a = %a@." N.pp a; | |
fprintf std_formatter "b = %a@." N.pp b; | |
fprintf std_formatter "a ∪ b = %a 和集合@." N.pp (N.union a b); | |
fprintf std_formatter "a ∩ b = %a 積集合@." N.pp (N.inter a b); | |
fprintf std_formatter "a \\ b = %a 差集合@." N.pp (N.diff a b); | |
fprintf std_formatter "|a| = %d aの大きさ@." (N.cardinal a); | |
fprintf std_formatter "|a ∪ b| = %d aの大きさ@." (N.cardinal (N.union a b)); | |
fprintf std_formatter "aの冪集合 %a@." N.pp_ss (N.powerset a); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment