Skip to content

Instantly share code, notes, and snippets.

@sugitach
Last active August 29, 2015 14:07
Show Gist options
  • Save sugitach/73e15c30103751d1e6af to your computer and use it in GitHub Desktop.
Save sugitach/73e15c30103751d1e6af to your computer and use it in GitHub Desktop.
(* 10.2 目的:任意の整数のリストから昇順に整列したリストを返す *)
(* ins_sort : int list -> int list *)
let rec ins_sort lst = match lst with
[] -> []
| first :: rest -> insert (ins_sort rest) first
(* テスト *)
let test2_1 = ins_sort [] = []
let test2_2 = ins_sort [3;5;1] = [1;3;5]
let test2_3 = ins_sort [5;3;8;1;7;4] = [1;3;4;5;7;8]
(* 目的:lstの昇順となる位置に整数nを挿入する *)
(* insert : int list -> int -> int list *)
let rec insert lst n = match lst with
[] -> [n]
| first :: rest -> if (first < n)
then first :: (insert rest n)
else n :: first :: rest
(* テスト *)
let test1 = insert [] 1 = [1]
let test2 = insert [1;3;5] 2 = [1;2;3;5]
let test3 = insert [3;5;8;10;12;13] 6 = [3;5;6;8;10;12;13]
let test4 = insert [1;3;4;7;8] 5 = [1;3;4;5;7;8]
(* 学生一人分のデータ *)
type gakusei_t = {name: string; tensuu: int; seiseki: string}
(*学生データのリスト*)
let gakusei1 = { name = "asai"; tensuu = 70; seiseki = "B"; }
let gakusei2 = { name = "kaneko"; tensuu = 85; seiseki = "A"; }
let gakusei3 = { name = "yoshida"; tensuu = 80; seiseki = "A"; }
let lst1 = []
let lst2 = [gakusei1]
let lst3 = [gakusei1; gakusei2]
let lst4 = [gakusei3; gakusei1; gakusei2]
let rec gakusei_insert lst n = match lst with
[] -> [n]
| first :: rest -> if (first.tensuu < n.tensuu)
then first :: (gakusei_insert rest n)
else n :: first :: rest
(* 目的:gakusei_t型のリストを受け取ってtensuuフィールドの順に整列したリストを返す *)
(* gakusei_sort : gakusei_t list -> gakusei_t list *)
let rec gakusei_sort lst = match lst with
[] -> []
| first :: rest -> gakusei_insert (gakusei_sort rest) first
(* テスト *)
let test1 = gakusei_sort [] = []
let test2 = gakusei_sort [gakusei1; gakusei2; gakusei3] = [gakusei1; gakusei3; gakusei2]
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 person_insert lst n = match lst with
[] -> [n]
| first :: rest -> if (first.name < n.name)
then first :: (person_insert rest n)
else n :: first :: rest
let rec person_sort lst = match lst with
[] -> []
| first :: rest -> person_insert (person_sort rest) first
let rec person_names lst = match lst with
[] -> []
| first::rest -> first.name :: person_names rest
let test1 = person_names (person_sort []) = []
let test2 = person_names (person_sort test_data1) = [
"hidaka";
"ishimaru";
"kanehara";
"kata";
"shikiuchi";
"tachikawa";
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment