hw 7 ml type relation Left Right Incomparable
스카이라인 • hw 7. ml – type relation = Left | Right | Incomparable – compare_pair • 타입: int*int -> relation – compare_tri • 타입: int*int->int*int -> relation 13
스카이라인 • compare_pair p 1 p 2 – p 1=p 2이면 Incomparable – p 1이 p 2를 지배하면 Left – p 2가 p 1를 지배하면 Right – 그 외의 경우 Incomparable 14
스카이라인 가격 compare_pair A C = Incomparable 거리
스카이라인 가격 compare_pair C D = Left 거리
스카이라인 가격 compare_pair G F = Right 거리
Find_min let rec find_ min l = match l with | [h] -> (h, []) | h: : t -> let (m, l') = find_min t in if h < m then (h, t) else (m, h: : l'); ; 21
Selection let rec selection l = match l with | [] -> [] | _ -> let (m, l') = find_min l in m : : (selection l'); ; 23
Split let rec split l = match l with | [] -> ([], []) | [h] -> ([h], []) | h: : t -> let (l 1, l 2) = split t in if List. length l 1 <= List. length l 2 then (h : : l 1, l 2) else (l 1, h : : l 2); ; 26
Merge let rec merge l 1 l 2 = match (l 1, l 2) with | ([], _) -> l 2 | (_, []) -> l 1 | (h 1 : : t 1, h 2 : : t 2) -> if h 1 < h 2 then h 1 : : (merge t 1 l 2) else h 2 : : (merge l 1 t 2); ; 28
Merge-sort Let rec merge_sort l = match l with | [] -> [] | [h] -> [h] | _ -> let (l 1, l 2) = split l in let l 1' = merge_sort l 1 in let l 2' = merge_sort l 2 in merge l 1' l 2' ; ; 30
- Slides: 30