Posted at

OCamlの練習 ソート

More than 1 year has passed since last update.


  • OCamlの練習

  • 野球成績型のリストを勝率順に並び替える


baseball.ml

(* 野球成績型 *)

type baseball_seiseki = {
name: string; (* 名前 *)
won: int; (* 勝ち数 *)
drawn: int; (* 引けわけ数 *)
lost: int; (* 負け数 *)
}

(* 2016データ *)
let hiroshima = {name = "広島"; won = 89; drawn = 2; lost = 52}
let kyojin = {name = "巨人"; won = 71; drawn = 3; lost = 69}
let dena = {name = "DeNA"; won = 69; drawn = 3; lost = 71}
let hanshin = {name = "阪神"; won = 64; drawn = 3; lost = 76}
let yakuruto = {name = "ヤクルト"; won = 64; drawn = 1; lost = 78}
let chuunichi = {name = "中日"; won = 58; drawn = 3; lost = 82}
let npb2016 = [hiroshima; kyojin; dena; hanshin; yakuruto; chuunichi]

(* 勝率計算 勝率 = 勝数 / (勝数 + 負数) *)
(* shouritsu : int -> int -> int *)
let shouritsu won lost = int_of_float(float_of_int(won) /. float_of_int(won + lost) *. 100.0)

(* 勝率TEST *)
let test1 = shouritsu 0 0 = 0
let test2 = shouritsu 1 0 = 100
let test3 = shouritsu 0 1 = 0
let test4 = shouritsu 100 50 = 66
let test5 = shouritsu 50 50 = 50

(* 勝率 降順位置に挿入 *)
(* baseball_seiseki_insert : baseball_seiseki list -> baseball_seiseki list *)
let rec baseball_seiseki_insert list target_team = match list with
| [] -> [target_team]
| ({name = loop_name; won = loop_won; drawn = loop_drawn; lost = loop_lost}) as loop_team :: rest ->
match target_team with {name = target_name; won = target_won; drawn = target_drawn; lost = target_lost} ->
if shouritsu target_won target_lost < shouritsu loop_won loop_lost then loop_team :: baseball_seiseki_insert rest target_team
else target_team :: list

(* 挿入TEST *)
let test_npb1 = [hiroshima; kyojin; dena; yakuruto; chuunichi]
let test_npb2 = [kyojin; dena; yakuruto]
let test1 = baseball_seiseki_insert [] hanshin = [hanshin]
let test2 = baseball_seiseki_insert test_npb1 hanshin = [hiroshima; kyojin; dena; hanshin; yakuruto; chuunichi]
let test3 = baseball_seiseki_insert test_npb2 chuunichi = [kyojin; dena; yakuruto; chuunichi]

(* 勝率降順ソート *)
(* baseball_seiseki_sort : baseball_seiseki list -> baseball_seiseki list *)
let rec baseball_seiseki_sort list = match list with
| [] -> []
| first :: rest -> baseball_seiseki_insert (baseball_seiseki_sort rest) first

(* 勝率降順ソートTEST *)
let test_npb1 = [hanshin; hiroshima; chuunichi; kyojin; dena; yakuruto;]
let test_npb2 = [chuunichi; kyojin; hanshin; hiroshima; dena; yakuruto]
let test1 = baseball_seiseki_sort [] = []
let test2 = baseball_seiseki_sort test_npb1 = npb2016
let test3 = baseball_seiseki_sort test_npb2 = npb2016



インサート Trace

# #trace baseball_seiseki_insert;;

baseball_seiseki_insert is now traced.
# baseball_seiseki_insert [hiroshima; kyojin; dena; yakuruto; chuunichi] hanshin;;
baseball_seiseki_insert <--
[{name = "広島"; won = 89; drawn = 2; lost = 52};
{name = "巨人"; won = 71; drawn = 3; lost = 69};
{name = "DeNA"; won = 69; drawn = 3; lost = 71};
{name = "ヤクルト"; won = 64; drawn = 1; lost = 78};
{name = "中日"; won = 58; drawn = 3; lost = 82}]
baseball_seiseki_insert --> <fun>
baseball_seiseki_insert* <--
{name = "阪神"; won = 64; drawn = 3; lost = 76}
baseball_seiseki_insert <--
[{name = "巨人"; won = 71; drawn = 3; lost = 69};
{name = "DeNA"; won = 69; drawn = 3; lost = 71};
{name = "ヤクルト"; won = 64; drawn = 1; lost = 78};
{name = "中日"; won = 58; drawn = 3; lost = 82}]
baseball_seiseki_insert --> <fun>
baseball_seiseki_insert* <--
{name = "阪神"; won = 64; drawn = 3; lost = 76}
baseball_seiseki_insert <--
[{name = "DeNA"; won = 69; drawn = 3; lost = 71};
{name = "ヤクルト"; won = 64; drawn = 1; lost = 78};
{name = "中日"; won = 58; drawn = 3; lost = 82}]
baseball_seiseki_insert --> <fun>
baseball_seiseki_insert* <--
{name = "阪神"; won = 64; drawn = 3; lost = 76}
baseball_seiseki_insert <--
[{name = "ヤクルト"; won = 64; drawn = 1; lost = 78};
{name = "中日"; won = 58; drawn = 3; lost = 82}]
baseball_seiseki_insert --> <fun>
baseball_seiseki_insert* <--
{name = "阪神"; won = 64; drawn = 3; lost = 76}
baseball_seiseki_insert* -->
[{name = "阪神"; won = 64; drawn = 3; lost = 76};
{name = "ヤクルト"; won = 64; drawn = 1; lost = 78};
{name = "中日"; won = 58; drawn = 3; lost = 82}]
baseball_seiseki_insert* -->
[{name = "DeNA"; won = 69; drawn = 3; lost = 71};
{name = "阪神"; won = 64; drawn = 3; lost = 76};
{name = "ヤクルト"; won = 64; drawn = 1; lost = 78};
{name = "中日"; won = 58; drawn = 3; lost = 82}]
baseball_seiseki_insert* -->
[{name = "巨人"; won = 71; drawn = 3; lost = 69};
{name = "DeNA"; won = 69; drawn = 3; lost = 71};
{name = "阪神"; won = 64; drawn = 3; lost = 76};
{name = "ヤクルト"; won = 64; drawn = 1; lost = 78};
{name = "中日"; won = 58; drawn = 3; lost = 82}]
baseball_seiseki_insert* -->
[{name = "広島"; won = 89; drawn = 2; lost = 52};
{name = "巨人"; won = 71; drawn = 3; lost = 69};
{name = "DeNA"; won = 69; drawn = 3; lost = 71};
{name = "阪神"; won = 64; drawn = 3; lost = 76};
{name = "ヤクルト"; won = 64; drawn = 1; lost = 78};
{name = "中日"; won = 58; drawn = 3; lost = 82}]
- : baseball_seiseki list =
[{name = "広島"; won = 89; drawn = 2; lost = 52};
{name = "巨人"; won = 71; drawn = 3; lost = 69};
{name = "DeNA"; won = 69; drawn = 3; lost = 71};
{name = "阪神"; won = 64; drawn = 3; lost = 76};
{name = "ヤクルト"; won = 64; drawn = 1; lost = 78};
{name = "中日"; won = 58; drawn = 3; lost = 82}]
#


ソート Trace

# #untrace baseball_seiseki_insert;;

baseball_seiseki_insert is no longer traced.
# #trace baseball_seiseki_sort;;
baseball_seiseki_sort is now traced.
# baseball_seiseki_sort [hanshin; hiroshima; chuunichi; kyojin; dena; yakuruto;];;
baseball_seiseki_sort <--
[{name = "阪神"; won = 64; drawn = 3; lost = 76};
{name = "広島"; won = 89; drawn = 2; lost = 52};
{name = "中日"; won = 58; drawn = 3; lost = 82};
{name = "巨人"; won = 71; drawn = 3; lost = 69};
{name = "DeNA"; won = 69; drawn = 3; lost = 71};
{name = "ヤクルト"; won = 64; drawn = 1; lost = 78}]
baseball_seiseki_sort <--
[{name = "広島"; won = 89; drawn = 2; lost = 52};
{name = "中日"; won = 58; drawn = 3; lost = 82};
{name = "巨人"; won = 71; drawn = 3; lost = 69};
{name = "DeNA"; won = 69; drawn = 3; lost = 71};
{name = "ヤクルト"; won = 64; drawn = 1; lost = 78}]
baseball_seiseki_sort <--
[{name = "中日"; won = 58; drawn = 3; lost = 82};
{name = "巨人"; won = 71; drawn = 3; lost = 69};
{name = "DeNA"; won = 69; drawn = 3; lost = 71};
{name = "ヤクルト"; won = 64; drawn = 1; lost = 78}]
baseball_seiseki_sort <--
[{name = "巨人"; won = 71; drawn = 3; lost = 69};
{name = "DeNA"; won = 69; drawn = 3; lost = 71};
{name = "ヤクルト"; won = 64; drawn = 1; lost = 78}]
baseball_seiseki_sort <--
[{name = "DeNA"; won = 69; drawn = 3; lost = 71};
{name = "ヤクルト"; won = 64; drawn = 1; lost = 78}]
baseball_seiseki_sort <--
[{name = "ヤクルト"; won = 64; drawn = 1; lost = 78}]
baseball_seiseki_sort <-- []
baseball_seiseki_sort --> []
baseball_seiseki_sort -->
[{name = "ヤクルト"; won = 64; drawn = 1; lost = 78}]
baseball_seiseki_sort -->
[{name = "DeNA"; won = 69; drawn = 3; lost = 71};
{name = "ヤクルト"; won = 64; drawn = 1; lost = 78}]
baseball_seiseki_sort -->
[{name = "巨人"; won = 71; drawn = 3; lost = 69};
{name = "DeNA"; won = 69; drawn = 3; lost = 71};
{name = "ヤクルト"; won = 64; drawn = 1; lost = 78}]
baseball_seiseki_sort -->
[{name = "巨人"; won = 71; drawn = 3; lost = 69};
{name = "DeNA"; won = 69; drawn = 3; lost = 71};
{name = "ヤクルト"; won = 64; drawn = 1; lost = 78};
{name = "中日"; won = 58; drawn = 3; lost = 82}]
baseball_seiseki_sort -->
[{name = "広島"; won = 89; drawn = 2; lost = 52};
{name = "巨人"; won = 71; drawn = 3; lost = 69};
{name = "DeNA"; won = 69; drawn = 3; lost = 71};
{name = "ヤクルト"; won = 64; drawn = 1; lost = 78};
{name = "中日"; won = 58; drawn = 3; lost = 82}]
baseball_seiseki_sort -->
[{name = "広島"; won = 89; drawn = 2; lost = 52};
{name = "巨人"; won = 71; drawn = 3; lost = 69};
{name = "DeNA"; won = 69; drawn = 3; lost = 71};
{name = "阪神"; won = 64; drawn = 3; lost = 76};
{name = "ヤクルト"; won = 64; drawn = 1; lost = 78};
{name = "中日"; won = 58; drawn = 3; lost = 82}]
- : baseball_seiseki list =
[{name = "広島"; won = 89; drawn = 2; lost = 52};
{name = "巨人"; won = 71; drawn = 3; lost = 69};
{name = "DeNA"; won = 69; drawn = 3; lost = 71};
{name = "阪神"; won = 64; drawn = 3; lost = 76};
{name = "ヤクルト"; won = 64; drawn = 1; lost = 78};
{name = "中日"; won = 58; drawn = 3; lost = 82}]
#