Skip to content

Instantly share code, notes, and snippets.

@sugitach
Created October 30, 2014 11:03
Show Gist options
  • Save sugitach/848088466993b0d7cff4 to your computer and use it in GitHub Desktop.
Save sugitach/848088466993b0d7cff4 to your computer and use it in GitHub Desktop.
(* 型定義 *)
type date_t = {
month : int;
day : int;
}
type person_t = {
name : string;
height : float;
weight : float;
birthday : date_t;
blood : string;
}
(* テスト用データ *)
let test_data1 = [
{
name = "ishimaru";
height = 1.73;
weight = 73.2;
birthday = { month = 10; day = 8; };
blood="A";
};
{
name = "tachikawa";
height = 1.60;
weight = 59.2;
birthday = { month = 12; day = 15; };
blood = "B";
};
{
name = "kanehara";
height = 1.60;
weight = 59.2;
birthday = { month = 12; day = 15; };
blood = "B";
};
{
name = "kata";
height = 1.60;
weight = 59.2;
birthday = { month = 12; day = 15; };
blood = "O";
};
{
name = "hidaka";
height = 1.60;
weight = 59.2;
birthday = { month = 12; day = 15; };
blood = "C";
};
{
name = "shikiuchi";
height = 1.60;
weight = 59.2;
birthday = { month = 12; day = 15; };
blood = "AB";
}
]
let rec ketsueki_shukei lst = match lst with
[] -> (0, 0, 0, 0, 0)
| { name = n; height = h; weight = w; birthday = b; blood = bl } :: rest ->
let (a, b, o, ab, other) = ketsueki_shukei rest in
if bl = "A" then (a + 1, b, o, ab, other)
else if bl = "B" then (a, b + 1, o, ab, other)
else if bl = "O" then (a, b, o + 1, ab, other)
else if bl = "AB" then (a, b, o, ab + 1, other)
else (a, b, o, ab, other + 1)
let t0 = ketsueki_shukei [] = (0, 0, 0, 0, 0)
let t1 = ketsueki_shukei test_data1 = (1, 2, 1, 1, 1)
(* 目的:person_t型を受け取って4つの血液型のうち最も人数の多かった血液型を返す *)
(* saita_ketsueki : person_t list -> string *)
let saita_ketsueki lst =
let (a, b, o, ab, other) = ketsueki_shukei lst in
if a >= b && a >= o && a >= ab && a >= other then "A"
else if b >= o && b >= ab && b >= other then "B"
else if o >= ab && o >= other then "O"
else if ab >= other then "AB"
else "Other"
(* テスト *)
let test1 = saita_ketsueki [] = "A"
let test2 = saita_ketsueki test_data1 = "B"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment