練習問題 7.1

例外処理機構を使って、オプション型を返り値とする、find 関数を書きなさい。
# find;;
– : ‘a -> ‘a list -> int option =
# find 7 [0; 8; 7; 3];;
– : int option = Some 3
# find 9 [0; 8; 7; 3];;
– : int option = None

解答その1

let find x =
    let rec find_in x e = function
        [] -> None
    | v :: rest when v = x -> Some e
    | _ :: rest ->
            find_in x (e+1) rest
    in
    find_in x 1
;;

解答その2

let find x =
    let rec find_in x e = function
        [] -> None
    | v :: rest ->
            if v = x then Some e
            else
                find_in x (e+1) rest
    in
    find_in x 1
;;

実行例

let test_find1 = find 7 [0; 8; 7; 3] = Some 3;;  (* true *)
let test_find1 = find 9 [0; 8; 7; 3] = None;;   (* true *)

どちらの解答でもいいんだろうけど、自分としては、その2のほうがわかりやすいなあ。