読者です 読者をやめる 読者になる 読者になる

一人 OCaml 勉強会 #5 match ... with

ocaml

Erlang で言うところの case ... of パターンマッチ。

どんなインデントがいいのかわからないなぁ、インデントサンプルは OMake でいいのかな。

とりあえず写経してみた。

let rec sum_list list =
  match list with
  | [] ->
      0
  | n :: rest ->
    n + (sum_list rest);;

let f x =
  match x with
  | (1, _) ->
    2
  | (_, 1) ->
    3
  | (1, 1) ->
    0
  | _ ->
    1;;

let rec max_list list =
  match list with
  | [x] ->
      x
  | n1 :: n2 :: rest ->
    if n1 > n2 then
      max_list (n1 :: rest)
    else
      max_list (n2 :: rest);;

let hd (x :: rest) = x
let tl (x :: rest) = rest;;

let rec nth n list =
  if n = 1 then
    hd list
  else
    nth (n - 1) (tl list)
let rec take n list =
  if n = 0 then
    []
  else
    (hd list) :: (take (n - 1) (tl list))
let rec drop n list =
  if n = 0 then
    list
  else
    drop (n - 1) (tl list);;

let null =
  function
  |  [] ->
    true
  | _ ->
    false;;

let rec append list1 list2 =
  match list1 with
  | [] ->
    list2
  | x :: rest ->
    x :: (append rest list2);;

let rec length =
  function
  | [] ->
      0
  | _ :: rest ->
    1 + length rest;;

(* これは遅い *)
let rec reverse =
  function
  | [] ->
      []
  | x :: rest ->
    append (reverse rest) [x];;

let rec revAppend list1 list2 =
  match list with
  | [] ->
    list2
  | x :: rest ->
    revAppend rest (x :: list2)

let rec map f =
  function
  | [] ->
    []
  | x :: rest ->
    f x :: map f rest;;

let rec forall p =
  function
  | [] ->
    true
  | x :: rest ->
    if p x then
      forall p rest
    else
      false
let rec exists p =
  function
  | [] ->
    false
  | x :: rest ->
    (p x) or (exists p rest);;

let rec fold_right f list e =
  match list with
  | [] ->
    e
  | x :: rest ->
    f x (fold_right f rest e)
let rec fold_left f e list =
  match list with
  | [] ->
      e
  | x :: rest ->
    fold_left f (f e x) rest;;

追記

@camlspotter 先生にインデントを教えて頂いたので修正。とてもすっと入ってくるようになった。