院卒新人サラリーマンのメモ代わり

備忘としてのメモを記載

reasonと戯れる

let rec insert = (lst: list(int), n: int): list(int) =>
  switch (lst) {
  | [] => [n]
  | [first, ...rest] =>
   switch (first < n) {
   | true  => [first, ...insert(rest, n)]
   | false => [n,...lst]
   };
  };

let rec insSort = (lst: list(int)): list(int) =>
  switch (lst) {
  | [] => []
  | [first, ...rest] => insert(insSort(rest), first)
  };

挿入法によるソートがこんな簡単にできるなんて
しかも、自分で作れた!

reasonのswitchは2つ意味がある。
1つ目は他の言語と同じif-elseのswicth
もう一つはパターンマッチングのswitch