プログラミングの基礎 問題13

ちょっとぼーっとしてしまった。ちょっとダメだったな。。

(* 人間一人分のデータ (身長、体重、誕生日、血液型)*)
type person_t = {
  name : string;
  height : float;
  weight : float;
  birthday : int * int;
  blood  : string;
}
(* person_tを受け取って、血液型の人数を返す *)
(* count_ketsueki : person_t list -> string -> int *)
let rec count_ketsueki xs blood_type = match xs with 
[] -> 0 
| { name = n; height = h; weight = w; birthday = b; blood = bl;}::rest
   -> ( if blood_type = bl then 1 else 0 ) + count_ketsueki rest blood_type 

(* testing *)
let a = { name = "p1"; 
           height = 1.0; 
           weight = 1.0; 
           birthday = (1,1); 
           blood = "A" }

let b = { name = "p2"; 
           height = 1.0; 
           weight = 1.0; 
           birthday = (1,1); 
           blood = "B" }

let ab = { name = "p3"; 
           height = 1.0; 
           weight = 1.0; 
           birthday = (1,1); 
           blood = "AB" }

let o  = { name = "p4"; 
           height = 1.0; 
           weight = 1.0; 
           birthday = (1,1); 
           blood = "O" }
let sample_list = [a;b;b;ab;ab;ab;o;o;o;o]

let test_count_ketsueki1 = count_ketsueki sample_list "A"  = 1
let test_count_ketsueki2 = count_ketsueki sample_list "B"  = 2 
let test_count_ketsueki3 = count_ketsueki sample_list "AB" = 3
let test_count_ketsueki4 = count_ketsueki sample_list "O"  = 4
let test_count_ketsueki5 = count_ketsueki [] "A" = 0
(* person_tを受け取って、namaeを返す *)
let getName person = match person with { name = n } -> n
(* リストを受け取って、人の名前のリストを返す *)
(* person_namae : person_t list -> string list *)
let person_namae xs = List.map getName xs

(* testing *)
let test_person_namae1 = person_namae [] = []
let test_person_namae2 = person_namae [a;b;ab;o] = ["p1";"p2";"p3";"p4"]


let add3 x = x + 3
let time2 x = x * 2

let compose fx gx x = fx (gx x)